简体   繁体   English

Array.map 的结果在 Ajax 请求中未定义

[英]Result of Array.map is undefined in Ajax request

I have very confusing behaviour in javascript.我在 javascript 中有非常令人困惑的行为。 The body of HTTP request is undefined=&undefined= . HTTP 请求的主体是undefined=&undefined=

var data = [1, 2].map(function(item) {
    return {
        Id: item,
        Quantity: 1
    }
});

$.ajax({
    url: "someUrl",
    type: "POST",
    dataType: "json",
    data: data
});

How can I prevent from losing data?如何防止数据丢失? Please advise.请指教。

The problem is that you are posting data with Content-Type:application/x-www-form-urlencoded header.问题是您使用Content-Type:application/x-www-form-urlencoded标头发布data Check documentation for $.ajax .检查$.ajax 的文档。 In this case data passed will be serialized with $.param method, which if you check documentation does the following:在这种情况下,传递的数据将使用$.param方法进行序列化,如果您检查文档,则会执行以下操作:

Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request.创建适用于 URL 查询字符串或 Ajax 请求的数组、普通对象或 jQuery 对象的序列化表示。 In case a jQuery object is passed, it should contain input elements with name/value properties.如果传递了 jQuery 对象,它应该包含具有名称/值属性的输入元素。

And now you know why the data is posted as现在你知道为什么数据被发布为

undefined=undefined&undefined=undefined未定义=未定义&未定义=未定义

Finally, if you want to post a JSON payload you can stringify data yourself and add application/json content-type:最后,如果你想发布一个 JSON 负载,你可以自己对数据进行字符串化并添加application/json内容类型:

$.ajax({
    url: "someUrl",
    type: "POST",
    dataType: "json",
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8"
});

The data attribute in ajax function is not the data returned from the ajax execution. ajax函数中的data属性不是ajax执行返回的数据。 It is the data sent to it.它是发送给它的数据。

Use the success callback function to manipulate the data returned使用success回调函数操作返回的数据

$.ajax({
    url: "someUrl",
    type: "POST",
    dataType: "json",
    data: data,
    success: function(dataReturned) {
       // Do something with dataReturned
    }
});

Take a look at the documentation看一下文档

You need to stringify your data object in order for it to be retrievable via $_POST at the destination.您需要对data对象进行字符串化,以便在目的地通过$_POST检索它。

var data = [1, 2].map(function(item) {
    return {
        Id: item,
        Quantity: 1
    }
});
$.ajax({
    async: false,
    url: 'someUrl',
    method: 'post',
    dataType: 'json',
    data: 'data=' + JSON.stringify(data) /** <---- here; add a to retrieve on other end; in this case 'data=' */
});

Then to access it within your someUrl you can decode it using json_decode() :然后要在您的someUrl访问它,您可以使用json_decode()对其进行解码

$json = json_decode($_POST['data']);

Becomes:变成:

$json = Array
(
    [0] => stdClass Object
        (
            [Id] => 1
            [Quantity] => 1
        )

    [1] => stdClass Object
        (
            [Id] => 2
            [Quantity] => 1
        )

)

echo $json[0]->Id; // 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM