ajax发送json到php,使用ajax将JSON发送到PHP

我想以json格式向php发送一些数据,并在php中进行一些操作。我的问题是我无法通过Ajax将json数据发送到我的php文件。请帮助我该怎么做。我已经尝试过这种方式..

$(function (){

$("#add-cart").click(function(){

var bid=$('#bid').val();

var myqty=new Array()

var myprice=new Array()

qty1=$('#qty10').val();

qty2=$('#qty11').val();

qty3=$('#qty12').val();

price1=$('#price1').val();

price2=$('#price2').val();

price3=$('#price3').val();

var postData =

{

"bid":bid,

"location1":"1","quantity1":qty1,"price1":price1,

"location2":"2","quantity2":qty2,"price2":price2,

"location3":"3","quantity3":qty3,"price3":price3

}

var dataString = JSON.stringify(postData);

$.ajax({

type: "POST",

dataType: "json",

url: "add_cart.php",

data: {myData:dataString},

contentType: "application/json; charset=utf-8",

success: function(data){

alert('Items added');

},

error: function(e){

console.log(e.message);

}

});

});

});

在PHP中,我使用:

if(isset($_POST['myData'])){

$obj = json_decode($_POST['myData']);

//some php operation

}

在php文件中添加print_r($ _ POST)时,它在firebug中显示array(0){}。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>