如何使用 $.ajax(jQuery 或 Zepto)发布对象数组 [英] How do I POST an array of objects with $.ajax (jQuery or Zepto)

查看:27
本文介绍了如何使用 $.ajax(jQuery 或 Zepto)发布对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Zepto 或 Jquery 中使用 $.ajax 发布一组对象.两者都表现出相同的奇怪错误,但我找不到我做错了什么.

I'd like to POST an array of objects with $.ajax in Zepto or Jquery. Both exhibit the same odd error, but I can't find what I'm doing wrong.

使用像RestEasy"这样的测试客户端发送数据时,数据会保存到服务器,我可以看到浏览器的网络面板中的请求被破坏了,所以我相信 JS 是罪魁祸首.

The data saves to the server when sent using a test client like 'RestEasy', and I can see the request getting mangled in the browser's net panel, so I believe JS is the culprit.

如果我发送一个对象数组作为 POST 的数据属性,它们将无法正确发送.

If I send an array of objects as the data property of a POST, they are not properly sent.

数据对象:

var postData = [
    { "id":"1", "name":"bob"}
  , { "id":"2", "name":"jonas"}
  ]

请求:

$.ajax({
  url: _saveDeviceUrl
, type: 'POST'
, contentType: 'application/json'
, dataType: 'json'
, data: postData
, success: _madeSave.bind(this)
//, processData: false //Doesn't help
});

在浏览器中看到的请求正文:

Request body as seen in the browser:

"bob=undefined&jonas=undefined"

使用 jQuery 和 Zepto 用于准备 POST 数据的 $.param 方法可以更直接地看到这一点.

This can be seen more directly by using the $.param method that both jQuery and Zepto use to prepare POST data.

$.param(
  [
    { "id":"1", "name":"bob"}
  , { "id":"2", "name":"jonas"}
  ]
)
// Output: "bob=undefined&jonas=undefined"

因此,这些库为复杂的后期数据所做的准备似乎与我预期的不同.

So it seems like the preparation that these libraries do for complex post data is different than I expect.

我看到了这个答案,但我不想将数据作为查询参数发送,因为我要发布大量内容.如何发送数组.ajax 使用 jQuery 发布?

I see this answer, but I don't want to send the data as a query param as I'm POSTing lots of content. How do I send an array in an .ajax post using jQuery?

使用 jQuery/Zepto 通过 POST 发送多个对象的正确方法是什么?

What is the correct way to send multiple objects over POST using jQuery/Zepto?

使用 $.ajax({... data: JSON.stringify(postData) ...}) 发送未损坏的内容,但服务器不喜欢这种格式.

Using $.ajax({... data: JSON.stringify(postData) ...}) sends non-mangled content, but the server doesn't like the format.

更新:似乎 JSON.stringify 发送格式正确的内容.问题是服务器端对它想要的对象的结构非常非常具体.如果我从对象中添加或删除任何属性,它将使整个过程失败,而不是使用匹配的属性.这很不方便,因为使用服务器发送的内容作为视图模型很好,但是视图模型会发生变化....仍在研究最佳解决方案.

Update: Seems like JSON.stringify sends correctly formatted content. The issue is that the server side is very, very specific about the structure of the object that it wants. If I add or remove any properties from the object, it will fail the whole process rather than using the properties that do match. This is inconvenient because it's nice to use server-sent content as a view model, but view models get changed. ...Still working on the best solution.

推荐答案

发送前一定要stringify.我过于依赖库,认为它们会根据我发布的内容类型正确编码,但它们似乎没有.

Be sure to stringify before sending. I leaned on the libraries too much and thought they would encode properly based on the contentType I was posting, but they do not seem to.

作品:

$.ajax({
    url: _saveAllDevicesUrl
,   type: 'POST'
,   contentType: 'application/json'
,   data: JSON.stringify(postData) //stringify is important
,   success: _madeSave.bind(this)
});

我更喜欢这种方法而不是使用像 $.toJSON 这样的插件,尽管它确实可以完成同样的事情.

I prefer this method to using a plugin like $.toJSON, although that does accomplish the same thing.

这篇关于如何使用 $.ajax(jQuery 或 Zepto)发布对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆