Access-Control-Allow-Origin不允许使用origin http:// localhost:1716 [英] Origin http://localhost:1716 is not allowed by Access-Control-Allow-Origin
问题描述
我有这个错误
XMLHttpRequest无法加载
http:// localhost:81 / Test / Service。 SVC / SetJSON
。 Access-Control-Allow-Origin不允许来源http:// localhost:1716
。
XMLHttpRequest cannot load
http://localhost:81/Test/Service.svc/SetJSON
. Originhttp://localhost:1716
is not allowed by Access-Control-Allow-Origin.
当我使用jquery调用wcf web服务时。
when I calls wcf web service using jquery.
$.ajax({
type: 'POST',
url: webmethod,
data: '{"data":"Darshana"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (e) {
alert("Unavailable");
}
});
为什么会这样?
任何帮助。 thanx ..
any help. thanx..
推荐答案
我不记得我是怎么得到这个错误的。但是,由于很多人遇到这个问题,我想发布我所做的事情。
I don't remember how I got this error and when. But, as lots of people having this problem I thought to post what I did.
WCF - IService
WCF - IService
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "SetJSON?data={data}")]
string SetJSON(string data);
WCF - 服务
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public string SetJSON(string data)
{
return data;
}
}
WCF - web.config
WCF - web.config
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP"
crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
....
<services>
<service name="RnDService.Service">
<endpoint address="" behaviorConfiguration="webHttpBehavior"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP"
contract="RnDService.IService" />
</service>
</services>
Jquery call
Jquery call
$.ajax({
type: "GET",
url: "http://localhost:81/Test/Service.svc/SetJSON?data=" + "{ \"dl\":" + datalist + " }",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function (data) {
alert(data.toString());
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
alert("Error Occured!");
}
});
不是100%确定是什么解决了我的问题。无论如何,这将有助于某人。 :)
not 100% sure what solved my problem. Anyway this will help someone. :)
这篇关于Access-Control-Allow-Origin不允许使用origin http:// localhost:1716的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!