Ajax POST返回错误请求400

时间:2015-03-29 03:13:05

标签: ajax post

我已经尝试了几个小时来调试对我的服务器的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客户端看起来像这样:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>My Application</title>
    <script type="text/javascript" src="/Scripts/jquery-2.1.1.min.js"</script>
    <script type="text/javascript" src="/Scripts/ajax.js"> </script>
    <script type="text/javascript" src="/Scripts/events.js"> </script>
</head>
<body>
    <button id="world">HelloWorld</button>
    <button id="you">HelloYou</button>
</body>
</html>

和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)

我找到了让它发挥作用的方法。我将方法从POST更改为GET。

对于数据,我查看了JQuery API文档:

    data: { name: "John"},

它有效。我很惊讶,我认为GET不会让我将数据推送到服务器。

干杯

Isidore

相关问题