0

New to using API's and not good at JS/Ajax. Sorry if it's a stupid question.

I have the following code, which I got from the developers documentation and updated slightly:

var token = getParameterByName('access_token');
    
    $.ajax({
        url: "https://api.myapp.de/api/v2/users/me",
        type: "GET",
        beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'bearer ' + token);},
        success: function(data) {
            console.log(data);
        }
    });

This works and gets my user information and logs it to the console.

However, I'm trying to build a web app(to learn to code better) that needs to update information by using PATCH. How do I do this using Ajax?

I need to PATCH the following information in the JSON file: "id": "idIwantToUpdate", "ringNumber": 4. I've googled a lot and found some sites that may contain the answer(fe https://www.rfc-editor.org/rfc/rfc7396), but it seems to be described too complex for me to get it. Have been at it for hours with pretty much 0 progress. Anyone able to explain it simple? Thanks!

Community
  • 1
  • 1
narodel
  • 5
  • 1
  • 7
  • See where it says `"GET"`? change that to `"PATCH"`, and a `data: {}` element with something interesting and party on. – Heretic Monkey Apr 06 '20 at 18:42
  • 1
    Does this answer your question? [Using JQuery PATCH to make partial update](https://stackoverflow.com/questions/38945677/using-jquery-patch-to-make-partial-update) – Heretic Monkey Apr 06 '20 at 18:42
  • Does this answer your Question: https://stackoverflow.com/questions/38945677/using-jquery-patch-to-make-partial-update – Ron Apr 06 '20 at 18:42
  • Partially, but in: var patch = { "data" : "New data" } how should I format the data? Like this? var patch = { "data" : {“id”: “newId”, “ringNumber”: 4} } – narodel Apr 06 '20 at 19:11

1 Answers1

0

With VanillaJS, you can also use external library Axios in your project to do it.

var token = getParameterByName('access_token');

fetch('https://api.myapp.de/api/v2/users/me', {
      headers: {
        "Authorization": `bearer${token}`,
        "Content-type": "application/json; charset=UTF-8"
      },
      method: 'PATCH',
      body: JSON.stringify({
        id: id,
        idIwantToUpdate: idyouwant,
        ringNumber: ringNumberYouwantToChange
      });
    });

Lucjan Grzesik
  • 739
  • 5
  • 17