ishan jain
ishan jain

Reputation: 681

Sencha Touch Ajax Request Error: Origin null is not allowed by Access-Control-Allow-Origin.

I am making an Ajax request to remote server and sending parameters as POST method. But I am getting following response:

**"MLHttpRequest cannot load http://rasovai.com/mobilecontact1.php?_dc=1369189135731. Origin null is not allowed by Access-Control-Allow-Origin.  "**

I read about this error and found out that it is because of CORS, so I add header to the request as follows:

 Ext.Ajax.defaultHeaders = {
                                        'Accept': 'application/json',
                                        'Accept': 'Access-Control-Allow-Origin: *',
                                        'Accept': 'Access-Control-Allow-Credentials:   true',
                                          'Accept': 'Access-Control-Allow-Methods: OPTIONS, GET, POST',
                                        'Accept': 'Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
                                    };

but still I am getting the same error in response.

I am able to hit the url on server, but not able to pass the parameters.

Can anyone help me in this regard?

Thanks Ishan jain

Upvotes: 1

Views: 5659

Answers (3)

Gendaful
Gendaful

Reputation: 5802

If you want to pass the params to the server then you can do it as following.

 Ext.Ajax.request({
            url : serverURL,
            jsonData : requestParams,  //  Object which encapsulates the request params
            method : 'POST',
            withCredentials: true,
            useDefaultXhrHeader: false,

           // List of header params can be sent as follows
            headers : {
                "Content-Type" : "application/json",
                "Accept" : "application/json",
                "Access-Control-Allow-Origin":"http://localhost:8080",
                "Authorization":auth
            },

            username : 'mobiliser',
            password : 'secret',
            success : function(response) {
             }
            failure : function ()
             {
             }

As you said in your question that, the server is on a different domain, you may have to use JSONP request.

Let me know if you have any questions.

Thanks- Gendaful

Upvotes: 0

ThinkFloyd
ThinkFloyd

Reputation: 5021

If you are using Chrome browser you can use --disable-web-security flag to allow cross domain requests and if you are building this into app this will work fine. Have a look at this thread for more details : How to use json proxy to access remote services during development

Upvotes: 0

idbehold
idbehold

Reputation: 17168

Those headers need to be sent by the server, not the client.

Upvotes: 2

Related Questions