Recover ajax response value

Asked

Viewed 878 times

1

I have a service that registers in a system (the code below)

  var userstocreate = [
     {
      username: '' + usuario + '',
      password: 'E@d123456',
      firstname: '' + primeiro +'',
      lastname: '' + lastname + '',
      email: '' + email + ''
    }];
    var data = {
      wstoken: token,
      wsfunction: functionname,
      moodlewsrestformat: 'json',
      users: userstocreate
     }
     var response = $.ajax(
     {
      type: 'POST',
      data: data,
      url: serverurl
      });

The answer of this service is thus...

 abort: (a)
always: ()
complete: ()
done: ()
error: ()
fail: ()
getAllResponseHeaders: ()
getResponseHeader: (a)
overrideMimeType: (a)
pipe: ()
progress: ()
promise: (a)
readyState: 4
responseJSON: Array[1]
responseText: "[{"id":29,"username":"arques_nathalia"}]"
setRequestHeader: (a,b)
state: ()
status: 200
statusCode: (a)
statusText: "OK"
success: ()
then: ()
__proto__: Object 

How can I save responseText values? that "[{"id":29,"username":"arques_nathalia"}]"

wanted to save in a variable at least the ID, is possible?

  • You redeem these values on callback of function Ajax, in the success, for example.

  • How? Do you have an example? I did so Success: Function (data) { var result = data; } but I cannot use this result variable

1 answer

1


The code is commented.

var userstocreate = [
{
   username: '' + usuario + '',
   password: 'E@d123456',
   firstname: '' + primeiro +'',
   lastname: '' + lastname + '',
   email: '' + email + ''
}];

var params = { // Troca o nome dessa variável 'data' para 'params'
   wstoken: token,
   wsfunction: functionname,
   moodlewsrestformat: 'json',
   users: userstocreate
}

var response = $.ajax(
{
   type: 'POST',
   data: params, // Coloque o 'params' aqui
   url: serverurl,
   success: function(data){
       console.log(data); // Aqui você resgata os valores 'data.responseText'
       console.log(data.responseText);
   }
});
  • Okay, I got it, but I need to just get the ID? How do I do? Look at the answer. " [Object] 0: Object id: 38 username: "gallindo_ramires" proto: Object length: 1 proto: Array[0]"

  • The return json it’s like a array. When you put one .(point) is determining the depth you want to reach. Then the data is the top of the iceberg, put a . to go deeper. In this case data[0].id. But from what I see in your case is a little different. Put in your post how is returning the json, is easier to view.

  • Thank you very much, Best impossible explanation, worked right!

  • Oops! Glad it worked out. I thought I was going to have a possible problem yet...

  • What would it be? I’m with a simple now, because I need this id value to use in another function, and I’m not getting...

  • Then you’ll have to put the ID in a variable, thus: var id = data[0].id. Then you can use it in another function below the function ajax. However, you will have to add a parameter in the ajax. It may be after url: serverurl. Puts , at the end of this line and below: async: false.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.