weixin_33696106 2015-12-15 20:54 采纳率: 0%
浏览 34

jQuery不发布数组数据

ive tried 10 different solutions to this issue on stackoverflow and none of them work, heres the issue, i need to post a array via ajax to a url simple right?

the array comes from post values like this $_post[xxx] which i store in a var.

i mocked up a js function, that has the same error, instead of passing the array i just included as a var in the function, problem is the data is not gettin posted to the url. pastebin link: http://pastebin.com/8ivWqJ8g

function generateImage() {
 var Imagedata = [];

Imagedata['id'] = 1;
Imagedata['type'] = 'some type';


        var url = '';
         $.ajax({
            url: url,
              processData: false,
             dataType: 'JSON',
              data:  JSON.stringify(Imagedata) ,

             method:'POST',
              async: false
            });
  • 写回答

2条回答 默认 最新

  • weixin_33694172 2015-12-15 20:58
    关注

    Named properties of arrays do not get serialised when you convert the data to JSON.

    Arrays are supposed to hold an ordered set of values, not an arbitrary set of name:value pairs.

    Use a plain object ({}) not an array.


    Asides:

    Don't use processData: false unless you are passing something that isn't a string or an object to data (e.g. a FormData object). Since you are passing a string, it is harmless, but pointless. If you were passing an object (which you probably should be, see below), it would break things.

    Don't use async: false. It is deprecated, and harms the user experience (since it locks up the JS thread until the response arrives).

    If you want to populate PHP's $_POST then don't format your data as JSON. Pass the object directly to data: without using JSON.stringify.

    If you do want to make a JSON formatted request, then don't forget to set the contentType.


    function generateImage() {
      var Imagedata = {
        id: 1,
        type: "some type"
      };
    
      var url = '';
    
      $.ajax({
        url: url,
        dataType: 'JSON',
        data: Imagedata,
        method: 'POST'
      });
    
      // or …
    
      $.ajax({
        url: url,
        dataType: 'JSON',
        contentType: "application/json",
        data: JSON.stringify(Imagedata),
        method: 'POST'
      });
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路