jQuery 1.4.4+ AJAX request - post empty array or object becomes string

后端 未结 2 1738
时光说笑
时光说笑 2020-12-20 20:00

I have a object in Javascript that I am trying to AJAX POST to a PHP script. Everything worked in jQuery 1.4.1 but now in 1.4.4 or above all empty arrays or empty objects ar

相关标签:
2条回答
  • 2020-12-20 20:27

    Try setting the traditional option to true:

    $.ajax({
        type: 'POST',
        traditional: true,
        url: 'ajax.php',
        data: obj,
        success: function(data) {
            alert(data);
        }
    });
    

    Have a look at the data and traditional options of the newer API.

    And remove the extra comma from after the success callback if you want things to work in IE7.

    0 讨论(0)
  • 2020-12-20 20:32

    try applying JSON.stringify to the parameters passed

     data: JSON.stringify ( obj ),
    

    Note you'll likely want to include the contentType: "application/json" option to prompt server side to process the data correctly.

    quoting : Why jQuery ajax does not serialize my object?

    traditional: true is completely wrong, since it can never handle object hierarchies. What you get instead is: ...&key=[object Object], which is javascript's toString() default result for all objects.

    0 讨论(0)
提交回复
热议问题