Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi developers I am new in ajax and first time consuming web api .
This is my json object to authenticate a user . how can i pass this in ajax call to get the authenticate token please help me
Hide Copy Code

JavaScript
POST /v2.0/tokens HTTP/1.1
User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
Host: identity.api.rackspacecloud.com
Accept: application/json
Content-Type: application/json
Content-Length: 54
 
{
   "auth":
   {
      "RAX-KSKEY:apiKeyCredentials":
      {
         "username": (1)"jsmith",
         "apiKey": (2)"aaaaa-bbbbb-ccccc-12345678"
      }
   }
}

My requirement is that I want to Post this json object to server and get the response from server but function always returns error condition I asked to google about this many times and found lots of solution and tried all those but still I am not able to post data
I am doing this but not able to consume please give me some solution

JavaScript
$.ajax({
                    url: "https://identity.api.rackspacecloud.com/v1.0/tokens",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Content-Type: application/json");
                    },
                    type: 'POST',
                    dataType: 'json',
                    accept:'application/json',
                    
                 //  processData: true,
                    data: JSON.stringify({ "auth": { "RAX-KSKEY:apiKeyCredentials": { username: 'jsmith', apiKey: 'aaaaa-bbbbb-ccccc-12345678' } } }),
                    success: function (response) {
                        alert(JSON.stringify(response));
                    },
                    error: function () {
                        alert("Cannot get data");
                    }
                });
Posted
Updated 8-Jun-15 5:20am
v3

1 solution

Hi,

How did you generate apiKey? Are you sure that key is still valid? If you know the answer to these questions, half of the problem is solved.

When you send the authentication details, you have to send it as part of the Request Header, not as data. From the code you provided I assume you are using basic authentication.

But, do you know that these credentials can easily be compromised? Because these are base64 encoded not encrypted. Do take that into consideration when you choose a authentication method.

Here are some articles that show how to send the authentication details in request header.

http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax[^]

http://blog.tompawlak.org/basic-authentication-jquery-ajax[^]

http://laravel.io/forum/12-11-2014-basic-auth-api-jquery-cors[^]

Try these and see if it works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900