Edgar
Edgar

Reputation: 927

send JSON POST and get a field

What am I doing wrong, and how to do it right?

Description of what I need to do:

POST https://api.chat.center/users with following JSON {"is_web_user": true, "email": "[email protected]", "full_name" : "user full name"} Get access_token field and save it.

How I am doing it:

<script type='text/javascript'>
var JsonData = {"is_web_user": true, "email": "[email protected]", "full_name" : "user full name"};

$.ajax({
url: 'https://api.chat.center/users',
data: {request:$.toJSON(JsonData)},
type: 'POST',
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
    var return_value=(data.request.access_token);
}                                  
});
</script>

Upvotes: 1

Views: 44

Answers (1)

Elias Nicolas
Elias Nicolas

Reputation: 775

Your error is: use of $.toJSON() if your need to send in a format like this {requests:["is_web_user": true, "email": "[email protected]", "full_name" : "user full name"]} Could you try this:

<script type='text/javascript'>
    var JsonData = {request:["is_web_user": true, "email": "[email protected]", "full_name" : "user full name"]};

        $.ajax({
        url: 'https://api.chat.center/users',
        data: JsonData,
        type: 'POST',
        dataType: 'json',
        jsonp: false,
        crossDomain: true,
        success: function(data) {
            var return_value=(data.request.access_token);
        }                                  
        });
        </script>

You could debug using console.log() see here console.log() or here Console.Log() you can look at your dev tools in your browser. Hope it helps.

Upvotes: 2

Related Questions