I am facing an error when calling a WCF method from jQuery.
The call returns just fine when I implement it on localhost but throws an access denied error when the service is hosted on a separate server.
Here are the steps to reproduce the error:-
-
Run the project locally and check if the service is running
e.g.:http://localhost:3369/AppsterWcfService.svc/DoWork -
Create a test client (test.html) page locally and run this project.
When clicked on the login button it works fine. -
Now host the service on a remote server and replace the url with the new url i.e. url of the hosted service.
-
Now run the test.html page, click on the button. Here I get access denied error.
I have tried various methods of accessing the server e.g.:-
- By using MVC
- Handler
- simple .aspx page
- Adding different parameters in the jquery ajax call and web.config file.
- given different permissions in iis. like network service, iuser, anonymous, guest etc.
I suppose it is some kind of cross-domain related issue with jQuery.
Any help will be appreciated.
Thanks,
-Sumit
For you to consume a cross domain WCF REST service using jQuery please find a sample below:
My Service looks as below:
Now my config entries for the above service looks as shown:
You need to note the
crossDomainScriptAccessEnabledattribute in the binding element “defaultRestJsonp” which takes care of determining the request to be for JSONP and appropriately converting the response to be wrapped in the callback method from the URL which comes as a query stringNow from your page do the below JavaScript that calls the above WCF REST service as shown:
Check out the jsonpCallback property in the $.ajax method call.
The raw request to the web service call looks as below:
And the raw response from the WCF REST service looks as below:
NOTE: When you perform a JSONP request your $.ajax methods error/complete/success are not called.