1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    16
    Vote Rating
    0
    Answers
    1
    fsuarez is on a distinguished road

      0  

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

    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 - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,525
    Vote Rating
    872
    Answers
    3564
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    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 @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    16
    Vote Rating
    0
    Answers
    1
    fsuarez is on a distinguished road

      0  

    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
    Vote Rating
    0
    Answers
    1
    fsuarez is on a distinguished road

      0  

    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

Thread Participants: 1

Tags for this Thread