2

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?

4

1 回答 1

4

问题不在于您的服务,而是拒绝调用另一个域中的 url 的浏览器。一种解决方案是在同一个域/站点下同时拥有页面和服务。

另一种解决方案是使用 jsonp: http: //www.codeproject.com/Articles/425702/Consume-WCF-Service-with-JSONP-and-SOAP-endpoints

于 2012-10-18T16:25:02.360 回答