我怎样才能解决这个错误
问题描述:
我试图调用使用AJAXWCF服务,下面是我的代码:我怎样才能解决这个错误
$.ajax({
url: "http://localhost/TestingServices/Service1.svc/GetData"
data: "{'value:1}",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
但在运行后它给了我以下错误:
XMLHttpRequest cannot load
http://localhost/TestingServices/Service1.svc/GetData.Response to
preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on
the requested resource. Origin 'null' is therefore not
allowed access. The response had HTTP status code 404.
灿任何人都可以帮我解决这个问题?
答
尝试2添加HttpProtocols在服务项目的web.config文件
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
答
请试试这个....
$.ajax({
type: "GET",
url: "http://localhost/TestingServices/Service1.svc/GetData",
dataType: "jsonp",
success: readData(data),
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
})
删除的contentType:“应用/ JSON的;字符集= UTF-8 “然后尝试,,, –
请参阅http://stackoverflow.com/questions/33820142/getting-request-doesnt-pass-access-control-check-no-access-control-allow-orig – RRR
现在,我得到dis错误:POST http://localhost/Wcf/service1.svc/GetData 415(无法处理消息,因为内容t type'application/x-www-form-urlencoded; charset = UTF-8'不是预期的类型'text/xml;字符集= UTF-8' )。 – tiya