weixin_33696106
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
            });

图片转代码服务由CSDN问答提供 功能建议

                    

ive在stackoverflow上尝试了10个不同的解决方案来解决这个问题,但是它们都不起作用,这是问题,我需要通过ajax将数组发布到url 简单吗?

该数组来自我存储在变量中的$ _post [xxx]这样的帖子值。

我模拟了一个具有相同错误的js函数,而不是传递我只是作为函数中的var包含的数组,问题是数据没有被发布到url中。 pastebin链接: http://pastebin.com/8ivWqJ8g

  function generateImage(){
  var Imagedata = [];

 Imagedata ['id'] = 1;
 Imagedata ['type'] ='某种类型';


         var url ='';
          $ .ajax({
             网址:url,
               processData:否,
              dataType:“ JSON”,
               数据:JSON.stringify(Imagedata),

              方法:“ POST”,
               异步:false
             });
  
     
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_33694172
    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'
      });
    
    }
    
    点赞 评论
  • weixin_33716154
    weixin_33716154 2015-12-15 21:00

    data should be an Object like this:

         $.ajax({
            url: 'http://...',
             dataType: 'JSON',
              data:  {id: 1, type: 'some type'} ,
    
             method:'POST',
              async: false
            });
    
    点赞 评论

相关推荐