-
6 Aug 2012 12:28 AM #1
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
The result of this isCode: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); } }); };
And if I comment out the jsonData line, I get this error:Code:- [COLOR=red !important]Resource failed to load Connection.js:317[/COLOR]
So the server is trying to process the request but failing as there is no login data.Code:- [COLOR=red !important]500 (Internal Server Error) Connection.js:317[/COLOR]
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!
-
Best Answer Posted by fsuarez
It was a CORS problem after all. To get my server supporting CORS Im using the following configuration:
In the web.config file (its a IIS server). I hope this helps someoneCode:<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>
-
8 Aug 2012 5:20 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,631
- Vote Rating
- 435
- Answers
- 3106
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
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
8 Aug 2012 1:25 PM #3
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
-
6 Dec 2012 8:16 PM #4
It was a CORS problem after all. To get my server supporting CORS Im using the following configuration:
In the web.config file (its a IIS server). I hope this helps someoneCode:<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>


Reply With Quote