Retrieve ajax response value

1

I have a service that registers on a system (from 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 response from this service stays like this ...

 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 the values of the responseText? this "[{" id ": 29," username ":" arques_nathalia "}]"

I wanted to store at least one ID in a variable, is it possible?

    
asked by anonymous 19.01.2016 / 11:23

1 answer

1

The code is commented out.

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);
   }
});
    
19.01.2016 / 11:36