douzuizhuo0587 2011-05-12 08:32 采纳率: 0%
浏览 60

将JSON字符串传递给$ .ajax并在PHP文件中访问它

I am trying to send JSON encoded string to PHP file through ajax.

I tried this but the $_POST is empty in the ajax target file.

var html = $.ajax({
            type: "POST",
            url: "getControl.php",
            data:js_post_array,
            dataType: 'json',
            async: false,
            success: function (data, status)
            {
              //alert('here');
              //$("#notiDesc").text(data.msg);              
            }
         }).responseText;

js_post_array contains json encoded string

{ 'business_name': 'test', 'business_type': 'R', 'type': '', 'total_sku_in_store': '',
 'speciality': '', 'first_name': '', 'last_name': '', 'title': '', 
 'responsible_for_wine_buying': '', 'responsible_for_events': '', 'address1': '' }

but in the getControl.php file I tried print_r($_POST) that shows an empty array.

But when I just copy this string and paste here data:js_post_array, instead of js_post_array then it works fine.

What I am doing wrong?

  • 写回答

3条回答 默认 最新

  • donglanzhan7151 2011-05-12 08:37
    关注

    it's because data accepts a string not other data type like array...it just append string as a query String in url and send to server.
    So if you pass an array then it's not going to break this into string, that's why it's giving you empty $_POST

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?