javascript 我的 jQuery POST 请求有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19865694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:04:32  来源:igfitidea点击:

what is wrong with my jQuery POST request?

javascriptjqueryajaxpost

提问by wx13

I am trying to authenticate with a remote service through jQuery. First, I verify that I can do this outside of the browser:

我正在尝试通过 jQuery 对远程服务进行身份验证。首先,我验证我可以在浏览器之外执行此操作:

 curl -X POST -H "Content-Type: application/json" -H "Accept: appliction/json" -d '{"username":"...","password":"..."}' http://example.com/auth

This successfully returns a token.

这成功地返回了一个令牌。

Now I try it with jQuery:

现在我用 jQuery 试试:

$.ajax({
    url: "http://example.com/auth",
    type: "POST",
    dataType: "json",
    contentType: "json",
    data: {username:"...",password:"..."},
    error: function(data){alert(JSON.stringify(data))},
    success: function(data){alert(JSON.stringify(data))}
});

I get a server error (500). Clearly I am doing something wrong. This is my first time trying to do a POST in jQuery, so I don't know how to identify what the problem is. What am I doing wrong here?

我收到服务器错误 (500)。显然我做错了什么。这是我第一次尝试在 jQuery 中执行 POST,所以我不知道如何确定问题所在。我在这里做错了什么?

P.S. I can successfully do a GET request through jQuery, if I already have a token:

PS 如果我已经有令牌,我可以通过 jQuery 成功执行 GET 请求:

$.ajax({
    url: "http://example.com/stuff?token=f42652adcbfe3ed9d59fae62b5267b8d",
    type: "GET",
    dataType: "json",
    error: function(data){alert(JSON.stringify(data))},
    success: function(data){alert(JSON.stringify(data))}
});

采纳答案by David

The only thing I notice is a difference in the data representations. Look at the data in the original request:

我唯一注意到的是数据表示的不同。查看原始请求中的数据:

-d '{"username":"...","password":"..."}'

And the data in the AJAX request:

AJAX 请求中的数据:

data: {username:"...",password:"..."}

The former wraps the keys in strings, whereas the latter doesn't. The whole thing should be a string too. Try:

前者将键包装在字符串中,而后者则不然。整个事情也应该是一个字符串。尝试:

data: '{"username":"...","password":"..."}'

This would be more consistent with JSON formatted datain general, I believe. Without wrapping the keys in strings, it may be a JavaScript object but it's not JSON data.

我相信,这通常与JSON 格式的数据更一致。如果不将键包装在字符串中,它可能是一个 JavaScript 对象,但它不是 JSON 数据。

回答by monastic-panic

Update: oops missed a comment saying stringify didn't work. I'll leave this for posterity

更新:哎呀错过了一条评论,说 stringify 不起作用。我会把这个留给后人

Sometimes you need to stringify the data when sending Json, otherwise jquery may serialize the object as a param string rather than as a whole object. It depends on how your server binds the request query to the object. though. Can you debug the server request or is that out of your hands?

有时需要在发送 Json 时对数据进行字符串化,否则 jquery 可能会将对象序列化为 param 字符串而不是整个对象。这取决于您的服务器如何将请求查询绑定到对象。尽管。您可以调试服务器请求还是您无法控制?

Try doing (provided you are on a semi modern browser) :

尝试做(假设您使用的是半现代浏览器):

   data: JSON.stringify({ your: data}) 

回答by wx13

Thank you everybody for your help. Each little piece of advice helped lead me to the solution. I said it in a comment already, but here is the solution:

谢谢大家的帮助。每一条小建议都帮助我找到了解决方案。我已经在评论中说过了,但这是解决方案:

  1. The dataType is correctly listed as "json", but the contentType should be listed as "application/json".

  2. The content must be wrapped in JSON.stringify.

  1. dataType 正确列为“json”,但 contentType 应列为“application/json”。

  2. 内容必须包含在 JSON.stringify 中。

回答by Mattias ?slund

Do you post to the same domain as the js was loaded from? If not you need to use jsonp and make sure the server explicitly accepts your request, I believe.

您是否发布到与加载 js 相同的域?如果不是,您需要使用 jsonp 并确保服务器明确接受您的请求,我相信。

回答by alexey

1) Open Network in Tools (f12)

1) 在工具中打开网络 (f12)

2) Select "Network"

2) 选择“网络”

3) Select error row

3) 选择错误行

4) Open "Body" on rights side

4) 在右侧打开“Body”

5) In title you can see error description eq.

5) 在标题中,您可以看到错误描述 eq。

<title>The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32)' in 'BosCatalog.Controllers.ProductsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.<br>Parameter name: parameters</title>

<title>The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32)' in 'BosCatalog.Controllers.ProductsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.<br>Parameter name: parameters</title>

回答by Krish R

500 Internal Server Error

500内部服务器错误

The server encountered an unexpected condition which prevented it from fulfilling the request.

服务器遇到意外情况,无法完成请求。

Json response from the url may be the reason, you can comment the stringfy function and alert the response. You can use try/catch method in response and check the error.

来自 url 的 Json 响应可能是原因,您可以注释 stringfy 函数并提醒响应。您可以使用 try/catch 方法作为响应并检查错误。