简体   繁体   中英

Call WCF Web Service from JavaScript

I've created a WCF Web Service and I want to access it straight from my html page using JavaScript. However I'm getting a cross-site scripting error.
Here's the error message:

XMLHttpRequest cannot load http://localhost:2875/MyWebService.svc/MyFunction. Origin http://TheCallingServer is not allowed by Access-Control-Allow-Origin.

I've found examples online describing how to do this from an aspx.net page, but I can't find anything about doing this from JavaScript in a HTML page.

What do I have to change in my WCF WebService to allow it to be called from JavaScript?

The problem is not in your service, is the browser who refuse to call an url in another domain. One solution is to have both, the page and the service under the same domain/site.

Another solution would be to use jsonp: http://www.codeproject.com/Articles/425702/Consume-WCF-Service-with-JSONP-and-SOAP-endpoints

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM