【问题标题】:Ajax POST returning an Bad Request 400Ajax POST 返回错误请求 400
【发布时间】:2015-06-02 05:31:46
【问题描述】:

我已经尝试了几个小时来调试对我的服务器的 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) + '}',

每次,我都会收到错误的错误请求 400。没有参数的相同功能 HelloWorld 工作正常。

我迷路了。

我与 Fiddler 核对了 HTML 请求/响应:

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

HTTP/1.1 400 错误请求

谢谢大家

伊西多尔

【问题讨论】:

    标签: ajax post


    【解决方案1】:

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

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

        data: { name: "John"},
    

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

    干杯

    伊西多尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2014-01-15
      • 1970-01-01
      • 2017-03-26
      相关资源
      最近更新 更多