-
28 Mar 2012 4:14 AM #1
Unanswered: Access wcf rest webservice with JSON
Unanswered: Access wcf rest webservice with JSON
Hello,
I am trying to use sencha to access a wcf webservice that receives and returns JSON format data. (Cross domain)
I am having the following message in the browser inspector:
OPTIONS http://"link to the resource" 200 (OK)
XMLHttpRequest cannot load http://"link to the resource" Origin http://localhost:81 is not allowed by Access-Control-Allow-Origin.
I have tried many things so far. I tryed to "bypassed" the security using this:
Code:WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "x-requested-with"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
My code for the call is the following:
I have commented the withCredentials line because it would give me this error: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true.Code:Ext.Ajax.request({ url: "http://link to the resource", method: "POST", params: { rData: JSON.stringify({ CompanyDb: "Company", PatientID: "100" }) }, //withCredentials: true, useDefautXhrHeader: false, success:function(resp){ var obj = JSON.decode(resp.reponseText); alert(obj); }, failure:function(resp){ alert("ERRO: " + resp.reponseText);}, headers:{ "Content-type": "application/json; charset=utf-8" } });
What am I doing wrong?
-
28 Mar 2012 5:18 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,710
- Vote Rating
- 436
- Answers
- 3113
Are you trying to use CORS?
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.
-
28 Mar 2012 6:00 AM #3
-
28 Mar 2012 6:10 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,710
- Vote Rating
- 436
- Answers
- 3113
If you are going to use withCredentials then the "Access-Control-Allow-Origin header needs to be something more than the wildcard '*' like the domain
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.
-
28 Mar 2012 6:27 AM #5
I am only using the withCredentials because I saw that in this guide: http://docs.sencha.com/touch/2-0/#!/guide/ajax in th section Cross Domain Requests, But I don't need to use the credentials for any reason.
All I wanted was to be able to call the wcf webservice properly
-
28 Mar 2012 11:22 AM #6
[works, but bad practise]
Are you going to deploy your application as an standalone app on a device and are you using your browser just for testing purposes? In that case you can start Chrome with an extra parameter --disable-web-security so there are no issues with cross domain scripting.
You could do this by creating a shortcut to Chrome and append the parameter to the shortcuts target.
Do not use a browser started this way to browse the internet, as it is not secure! Use at own risk!
[/works, but bad practise]
edit your web.config to include the following:
replace YourDomainHere with your domain, ip, localhost or *Code:<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="YourDomainHere" /> </customHeaders> </httpProtocol> </system.webServer>Last edited by joostvanhassel; 28 Mar 2012 at 11:29 AM. Reason: added better solution
-
17 Sep 2012 9:18 AM #7
-
17 Sep 2012 12:26 PM #8


Reply With Quote