1

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": "test@email.com", "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": "test@email.com", "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>
Edgar
  • 898
  • 2
  • 12
  • 36
  • What is the error you get? – ItayB Sep 19 '15 at 12:52
  • What is the request? Did you try just: data: JsonData, – ItayB Sep 19 '15 at 12:54
  • I took this code from a tutorial, and substituted it with my data from description. I thought "request" is required to send a JSON? – Edgar Sep 19 '15 at 13:28
  • 2
    `jsonp` doesn't support `POST`, only `GET`. If API is expecting POST and it isn't CORS enabled would need to use a proxy – charlietfl Sep 19 '15 at 13:51
  • 1
    I'm not sure it is relevant here, but my suggestion is to use chrome extention called 'REST Client' and try to communicate with the server first and see the response before diving into js implementation.. – ItayB Sep 19 '15 at 16:49

1 Answers1

2

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

<script type='text/javascript'>
    var JsonData = {request:["is_web_user": true, "email": "test@email.com", "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.

Community
  • 1
  • 1
Elias Nicolas
  • 775
  • 13
  • 26
  • Thank you. I tried your code, but I get error: {"errors":[{"userMessage":"Email is not provided","internalMessage":"Email is not provided"}]} and Status: 400 Bad Request – Edgar Sep 19 '15 at 23:26
  • I Don't how is formatted your request. But it seems to be the problem with the formatted code. – Elias Nicolas Sep 19 '15 at 23:58