我有两个网站
SysteTestWebsite包含一个Web服务“Service.svc”,其方法如下
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]<br>
public class Service
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public string SyncJson(string p1)
{
return p1;
}
// Add more operations here and mark them with [OperationContract]
}
我想从RemoteWebsite的HTML页面访问此方法,以下是此HTML页面的代码。
function SyncJson() {
var sPhone = "test data";
var data = { p1: sPhone };
data = JSON.stringify(data);
$.ajax({
type: "POST",
contentType: "application/json;",
url: "http://mydomain:12887/Service.svc/SyncJson",
data: data,
dataType: 'json',
success: function () {
alert('success');
},
error: function (msg) {
console.log(msg);
alert(msg.d);
}
});
}
<p>
Remote site
<input type="button" onclick="SyncJson()" value="Send Test Data" />
</p>
但我收到了这个错误:
NetworkError:405方法不允许 - http://abc.com/Service.svc/SyncJson
答案 0 :(得分:0)
这是跨域请求。您应该在创建服务时考虑这一点。 阅读更多关于CORS的信息。