Results 1 to 4 of 4

Thread: Cant POST JSON data to https REST API via Ext.Ajax while GET is possible

  1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    16
    Answers
    1

    Default Answered: Cant POST JSON data to https REST API via Ext.Ajax while GET is possible

    Hi,

    Im trying to use Ext.Ajax to post some data as JSON to a Rest API server. Its a cross domain request but the server uses CORS. Im able to do GET and retrieve JSON data with no problems but when trying to do a POST it fails. In the following code I try to do a login
    Code:
         login = function (callerInstance, user, pass, onLogin) {
            Ext.Ajax.setUseDefaultXhrHeader(false); 
            var url = SERVER + "login";
            var data = {};
            data.username = user;
            data.password = pass;
            Ext.Ajax.request({
                url: url,
                method: 'POST',
                jsonData: data,
                success: function(response, opts) {
                    console.log(TAG + 'responseText: ' + response.responseText);
                    onLogin.call(callerInstance, LOGGED_IN, response.responseText);
                },
                failure: function(response, opts) {
                    console.log(TAG + 'server-side failure with status code ' + response.status);
                    console.log(TAG + 'responseText: ' + response.responseText);
                    onLogin.call(callerInstance, NOT_LOGGED_IN, response.responseText);
                }
            });
        };
    The result of this is
    Code:
    
    HosRestApi: server-side failure with status code 0 
    And if I comment out the jsonData line, I get this error:
    Code:
    
    
    HosRestApi: server-side failure with status code 500
    HosRestApi: responseText: {"errorText":"Internal Server Error: Object reference not set to an instance of an object."}
    So the server is trying to process the request but failing as there is no login data.
    So Im not sure why is the first error I mentioned happening or where is happening, or whats the problem.
    The URL is https, does that makes any difference?
    Please help me on this one!

  2. It was a CORS problem after all. To get my server supporting CORS Im using the following configuration:

    Code:
    <customHeaders>
    	<add name="Access-Control-Allow-Origin" value="*" />
    	<add name="Access-Control-Allow-Methods" value="GET, POST, DELETE, OPTIONS" />
    	<add name="Access-Control-Allow-Headers" value="origin, content-type, x-requested-with" />
    </customHeaders>
    In the web.config file (its a IIS server). I hope this helps someone

  3. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,448
    Answers
    3997

    Default

    Did you check the request in the network tab of the dev tools to see your request and how things are sent?
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  4. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    16
    Answers
    1

    Default

    I did but I got no response when doing the POST. As I dont have acces to the server I want to communicate with, I asked to check the server configuration and the header ACCES-CONTROL-ALLOW-METHOD is not seted to allow POST, actually is not seted at all. So maybe is a CORS problem after all, but I dont know why I dont get an error saying that Im not allowed to do the request. Anyway, they wont change it in a couple of weeks so I will tell you later what happened.
    Thanks for your help

  5. #4
    Sencha User
    Join Date
    Jun 2012
    Posts
    16
    Answers
    1

    Default

    It was a CORS problem after all. To get my server supporting CORS Im using the following configuration:

    Code:
    <customHeaders>
    	<add name="Access-Control-Allow-Origin" value="*" />
    	<add name="Access-Control-Allow-Methods" value="GET, POST, DELETE, OPTIONS" />
    	<add name="Access-Control-Allow-Headers" value="origin, content-type, x-requested-with" />
    </customHeaders>
    In the web.config file (its a IIS server). I hope this helps someone

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •