通过AJAX POST JSON数据发送一个空数组 [英] POST json data via ajax sends an empty array

查看:711
本文介绍了通过AJAX POST JSON数据发送一个空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试后通过AJAX的数据,这是我的信息:

  VAR jsondata =
                {地址 : [
                {ID:ADDID的StreetAddress:的StreetAddress,城市:城市,状态:国家拉链code:拉链code,纬度:纬度},
            ]
            };

    VAR jsontosend = JSON.stringify(jsondata,空,2);
 

AJAX功能:

  $。阿贾克斯({
                键入:POST,
                网址:process.php
                的contentType:应用/ JSON的;字符集= UTF-8,
                数据类型:JSON,
                数据:jsontosend,
                成功:函数(MSG){
                   警报(味精);
                          }
             });

            返回false;

            警报(数据派);
 

}

在PHP结束,当我的print_r($ _ POST),它只是说

 阵列(0){
    }
 

我报警(jsontosend)及其使用后评判出我的一切完美,以及在萤火虫,它显示了所有在一个完美的清洁方式发送的PARAMS。

传递任何数据的唯一方法是,如果我用GET方法。

任何意见大大AP preciated!

编辑:从萤火添加POST数据。 这是什么正从警报功能提醒:

  {地址:[{ID:1473294的StreetAddress:3784霍华德大道,城市:华盛顿,状态:DC ,拉链code:20895,纬度:39.027820587}]}
 

这是什么Firebug是它呈现什么用POST方法时传递:

  myData=%7B%0A++++%22address%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22id%22%3A+76076%2C%0A++++++++++++%22streetaddress%22%3A+%223784+Howard+Ave%22%2C%0A++++++++++++%22city%22%3A+%22Washington+DC%22%2C%0A++++++++++++%22state%22%3A+%22DC%22%2C%0A++++++++++++%22zip$c$c%22%3A+20895%2C%0A++++++++++++%22latitude%22%3A+39.027820587%0A++++++++%7D%0A++++%5D%0A%7D
 

这是为$ _ POST的var_dump响应:

 阵列(0){
 

}

这是$ _ POST一个的var_dump ['myData的']

  NULL
 

解决方案

我怀疑你使用的contentType属性的方式。尝试取出的contentType。默认的内容类型是 应用程序/ x-WWW的形式urlen codeD( http://api.jquery.com/ jQuery.ajax / )。

此外,使用类似于{MYDATA:jsontosend}。为您的数据属性

  $。阿贾克斯({
            键入:POST,
            网址:process.php
            //的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON,
            数据:{MYDATA:jsontosend},
            成功:函数(MSG){
               警报(味精);
                      }
         });
 

I'm trying to post data via ajax, this is my info:

    var jsondata =
                {"address" : [
                { "id": addid, "streetaddress": streetaddress, "city": city, "state": state,  "zipcode": zipcode, "latitude": latitude},
            ]   
            };  

    var jsontosend = JSON.stringify(jsondata, null, 2);

ajax function:

    $.ajax({
                type: "POST",
                url: "process.php",
                contentType: "application/json; charset=utf-8",
                dataType: "JSON",
                data: jsontosend,
                success: function(msg){
                   alert(msg);
                          }
             });

            return false;

            alert('Data sent');

}

on the php end, when i print_r($_POST) it just says

    array(0) {
    }

I'm alerting (jsontosend) and its showing me everything perfectly, as well as in firebug using post mothod, its showing all the params sent in a perfect clean manner.

The only way it passes any data is if I use GET method.

Any advice is greatly appreciated!

EDIT: adding POST data from firebug. this is whats being alerted from the alert function:

    {"address":[{"id":1473294,"streetaddress":"3784 Howard Ave","city":"Washington DC","state":"DC","zipcode":20895,"latitude":39.027820587}]}

this is what firebug is showing as whats being passed when using POST method:

    myData=%7B%0A++++%22address%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22id%22%3A+76076%2C%0A++++++++++++%22streetaddress%22%3A+%223784+Howard+Ave%22%2C%0A++++++++++++%22city%22%3A+%22Washington+DC%22%2C%0A++++++++++++%22state%22%3A+%22DC%22%2C%0A++++++++++++%22zipcode%22%3A+20895%2C%0A++++++++++++%22latitude%22%3A+39.027820587%0A++++++++%7D%0A++++%5D%0A%7D

and this is the response for var_dump of $_POST:

    array(0) {

}

this is a var_dump of $_POST['myData']

    NULL

解决方案

I'm skeptical of the way you're using the contentType property. Try taking out contentType. The default content type is application/x-www-form-urlencoded (http://api.jquery.com/jQuery.ajax/).

Also, use something like {mydata: jsontosend} for your data property.

$.ajax({
            type: "POST",
            url: "process.php",
            //contentType: "application/json; charset=utf-8",
            dataType: "JSON",
            data: {mydata: jsontosend},
            success: function(msg){
               alert(msg);
                      }
         });

这篇关于通过AJAX POST JSON数据发送一个空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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