0

I am trying to authenticate my user using a POST request. But when I call to the rest call through jQuery it goes to the error state and returns error code 0.

When I call to the same rest call using postman or rest client, it gives me output as expected.

My jQuery is below,

$(document).ready(function() {
alert('started');
var dataType="application/json";
var data = {userName:"admin",password:"admin"};
$.ajax({
    type: "POST",
    url: "https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate",
    data: data,
    async : false,
    success: function(){alert("success")},
    error: function(){alert("error")},
    complete: function(){alert("complete")},
    statusCode: { 
        0: function() { alert( "0 : What happened here" )},
        404: function() { alert( "404 : Page not found" )},
        500: function() { alert( "500 : Internal server error" )}
    },
    dataType: dataType

}).then(function(data) {
   //handle the user here
});
});

I don't know why this returns http error 0. I called to https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate with userName=admin&password=admin and got the following output.

{
    Users: {
      User: {
        userName: "admin"
        userRoleId: 13268
      }
    }
}

Any ideas on this? Thanks in advance.!!

Madusanka
  • 2,968
  • 5
  • 30
  • 41
  • 1
    You are trying to sent a cross domain ajax request and and target resource is not supporting CORS that is the problem – Arun P Johny Oct 16 '14 at 06:12
  • 1
    Your browser console should show an error like `XMLHttpRequest cannot load https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. ` – Arun P Johny Oct 16 '14 at 06:13

1 Answers1

1
  • Make sure that your domain origin is allowed in rest service server located in appserver.dev.cloud.wso2.com
  • set globally jQuery.support.cors = true;
  • Change your dataType js var before ajax to var dataType="json";
  • setHeaders if anything required with headers ajax property

Now your script looks like:

    $(document).ready(function () {
        $.support.cors = true;
        var dataObject = { userName: "admin", password: "admin" };
        $.ajax({
            type: "POST",
            url: "https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate",
            data: dataObject,
            async: false,
            success: function () { alert("success") },
            error: function () { alert("error") },
            complete: function () { alert("complete") },
            statusCode: {
                0: function () { alert("0 : What happened here") },
                404: function () { alert("404 : Page not found") },
                500: function () { alert("500 : Internal server error") }
            },
            dataType: "json"
            , headers: { SecurityToken: securityTokenValue } // or set any header info here.
        }).then(function (data) {
            //handle other logic
        });
    });

Also check other similar posts:
How to get a cross-origin resource sharing (CORS) post request working

Community
  • 1
  • 1
Koti Panga
  • 3,660
  • 2
  • 18
  • 21