ajax post 返回错误信息,Ajax POST返回错误请求400

我已经尝试了几个小时来调试对我的服务器的Post Ajax调用。

我有2个POST方法:HelloWorld和HelloYou。

相同的代码,唯一的区别是HelloYou将一个字符串作为参数:

namespace WebService

{

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class Service

{

[OperationContract]

[WebInvoke(Method = "POST",

BodyStyle = WebMessageBodyStyle.WrappedRequest,

ResponseFormat = WebMessageFormat.Json,

RequestFormat = WebMessageFormat.Json)]

public string HelloWorld()

{

return "Hello World";

}

[OperationContract]

[WebInvoke(Method = "POST",

BodyStyle = WebMessageBodyStyle.WrappedRequest,

ResponseFormat = WebMessageFormat.Json,

RequestFormat = WebMessageFormat.Json)]

public string HelloYou(string name)

{

return string.Format("Hello {0}",name);

}

}

}

HTML客户端看起来像这样:

My Application

HelloWorld

HelloYou

和Ajax调用:

$(document).ready(function () {

$('#world').click(function () {

HelloWorld();

});

$('#you').click(function () {

HelloYou();

});

});

baseAddress = "http://localhost:53016/Service.svc/ajax/";

function GetURL(method) {

return baseAddress + method;

}

function HelloWorld() {

$.ajax({

async: false,

url: GetURL("HelloWorld"),

dataType: 'json',

type: 'POST',

data: null ,

processdata: true,

contentType: "application/json;charset-uf8"

})

.done(function(data) {

alert(data.d);

})

.fail(function (xhr, status, errorThrown) {

alert(status + errorThrown);

});

}

function HelloYou() {

$.ajax({

async: false,

url: GetURL("HelloYou"),

dataType: 'json',

type: 'POST',

data: JSON.stringify('{"name": "Chris"}'),

processdata: true,

contentType: "application/json;charset-uf8"

})

.done(function (data) {

alert(data.d);

})

.fail(function (xhr, status, errorThrown) {

alert(status + errorThrown);

});

}

我尝试了几种不同的方法将参数传递给Ajax调用:

data: JSON.stringify('{"name": "Chris"}'),

data: '{"name": "Chris"}',

data: '{name: "Chris"}',

var name ="Chris"

data: '{name: ' + JSON.stringify(name) + '}',

每次我收到错误Bad Request 400.没有参数的相同函数HelloWorld工作正常。

我迷路了。

我使用Fidler检查了HTML请求/响应:

POST /Service.svc/ajax/HelloYou HTTP / 1.1

HTTP / 1.1 400错误请求

全部谢谢

伊西多尔

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值