无法将Access-Control-Allow-Origin添加到我的WCF库项目中 [英] Could not add Access-Control-Allow-Origin to my WCF library Project
问题描述
我试图理解为什么这个叫ajax的电话不起作用
I'm trying to understand why this ajax called doesn't work
$.ajax({
type: 'GET',
url: "http://localhost:8732/Design_Time_Addresses/InMotionGIT_NT.Address.Service/AddressService/json/capitalize",
data: { streetAddress : JSON.stringify(streetAddress) , consumer : JSON.stringify(consumer)} ,
datatype: "jsonp",
success: function (data) {
$('body').append('<div>'+data.IDblah+' '+ data.prueba+'</div>');
alert(data.IDblah);
}
服务接收到的数据已正确接收,并且响应正确.为什么我做错了?
The service receive the data is correctly received and the response it's correct. Why am I doing wrong?
我尝试将此属性添加到称为的ajax中,但未成功crossDomain : true
I tried adding this property to the ajax called but without success crossDomain : true
[OperationContract()]
[WebInvoke(Method="GET", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
public string Capitalize(StreetAddress streetAddress,ConsumerInformation consumer)
我得到的错误是常见的
XMLHttpRequest cannot load Origin http://localhost:50816 is not allowed by Access-Control-Allow-Origin.
更新
我试图通过在我的App.config
文件中添加配置来将标头添加到响应中,但是没有成功
I tried to add the header to the response by adding the configuracion in my App.config
file but without success
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
推荐答案
此链接将帮助您: http://enable- cors.org/
您需要在发送回客户端的响应中添加以下标头:
You need to add the following headers in your response that is sent back to the client:
//允许所有域
Access-Control-Allow-Origin: *
OR
//允许特定域
Access-Control-Allow-Origin: http://example.com:8080 http://foo.example.com
这篇关于无法将Access-Control-Allow-Origin添加到我的WCF库项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!