Ajax post HTML 405,Ajax使用POST方式请求WCF的问题,以POST方式报405

本文介绍了一个关于使用jQuery AJAX调用WCF服务时遇到的跨域问题,详细展示了错误信息及WCF服务端代码配置,并提供了两种不同格式的数据发送方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

2jquery-1.11.0.min.js:4 OPTIONS http://202.115.195.241:8068/Service1.svc/hello1 405 (Method Not Allowed)

http://202.115.195.241:8068/Service1.svc/hello1: Response for preflight has invalid HTTP status code 405.

WCF代码及WebConfig配置:

[ServiceContract]

public interface IService1

{

[OperationContract, WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "hello/{nie}")]

string DoWork(string nie);

[OperationContract, WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "hello1")]

string DoWork1();

}

//实现

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class Service1 : IService1

{

public string DoWork(string nie)

{

return jss.Serialize("hello" + nie);

}

public string DoWork1()

{

return jss.Serialize("hello");

}

}

// 配置文件Webconfig

JQ访问WCF代码:

function bb() {

$.ajax

({

type: "POST",

url: "http://xxx/Service1.svc/hello",

contentType: "application/json",

data: '{"nie":"aa"}',

timeout: 10000,

dataType: "json",

success: function (response) {

alert(response)

},

error: function (xhr, ajaxOptions, thrownError) {

alert(xhr.statusText);

alert(thrownError);

}

});

}

$.ajax

({

type: "POST",

url: "http://XXX/Service1.svc/hello",

contentType: "application/json",

data: JSON.stringify({ "nie": "aa"}),

timeout: 10000,

dataType: "json",

success: function (response) {

alert(response)

},

error: function (xhr, ajaxOptions, thrownError) {

alert(xhr.statusText);

alert(thrownError);

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值