C# 起源http://myurl2/client wcf post服务中的访问控制允许来源不允许
我要走了 无法加载XMLHttpRequest。访问控制不允许原点允许原点。 调用用WCF Rest编写的Post服务时,我的客户端Javascript应用程序中出现此错误 我的服务C# 起源http://myurl2/client wcf post服务中的访问控制允许来源不允许,c#,wcf,rest,xmlhttprequest,cross-domain,C#,Wcf,Rest,Xmlhttprequest,Cross Domain,我要走了 无法加载XMLHttpRequest。访问控制不允许原点允许原点。 调用用WCF Rest编写的Post服务时,我的客户端Javascript应用程序中出现此错误 我的服务 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "post")]
bool JSONDataPost(string Value);
public bool JSONDataPost(string Value)
{ //code to Create the Person goes here
return true;
}
我的客户要求是
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://myurl.com/RestServiceImpl.svc/post', true);
xhr.setRequestHeader("Content-Type", "application/jsonp;charset=UTF-8");
xhr.onload = function () {
// do something to response
console.log(this.responseText);
};
xhr.send(JSON.stringify("TEST"));
您需要获得
http://myurl.com/RestServiceImpl.svc/post
发送OPTIONS
动词请求时,使用CORS
标题进行响应。我建议您切换到ASP.Net Web API
,因为您可以更好地控制REST谓词响应。另外,application/jsonp
不是有效的内容类型,“TEST”
不是有效的JSON。您应该分别使用application/json
和{“TEST”:“helloworld!”}
。