我想将一些json格式的数据发送到php,并在php中进行一些操作。我的问题是我无法通过 ajax 将 json 数据发送到我的 php 文件。请帮助我该怎么做。我试过这种方式..
<script>
$(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);
}
});
});
});
</script>
在PHP中,我使用:
if(isset($_POST['myData'])){
$obj = json_decode($_POST['myData']);
//some php operation
}
在 php 文件中添加 print_r($_POST) 时,它会在 firebug 中显示 array(0) {}。
丢掉contentType: "application/json; charset=utf-8",
. 您不是向服务器发送 JSON,而是发送普通的 POST 查询(恰好包含 JSON 字符串)。
这应该使你所拥有的工作。
问题是,您根本不需要在这里使用JSON.stringify
或json_decode
。 只需做:
data: {myData:postData},
然后在 PHP 中:
$obj = $_POST['myData'];
这是因为$_POST
预先填充了表单数据。
要获取 JSON 数据(或任何原始输入),请使用 php://input
。
$json = json_decode(file_get_contents("php://input"));
要使用 json 和 ajax 将 javascript obj 发送到 php:
.js:
var dataPost = {
"var": "foo"
};
var dataString = JSON.stringify(dataPost);
$.ajax({
url: 'server.php',
data: {myData: dataString},
type: 'POST',
success: function(response) {
alert(response);
}
});
要在 PHP 中使用该对象:
$obj = json_decode($_POST["myData"]);
echo $obj->var;
通过$_POST
变量获取值,那么你不应该将contentType指定为"application/json"
,而应该使用默认的"application/x-www-form-urlencoded; charset=UTF-8"
:
JavaScript:
var person = { name: "John" };
$.ajax({
//contentType: "application/json", // php://input
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // $_POST
dataType : "json",
method: "POST",
url: "http://localhost/test/test.php",
data: {data: person}
})
.done(function(data) {
console.log("test: ", data);
$("#result").text(data.name);
})
.fail(function(data) {
console.log("error: ", data);
});
.PHP:
<?php
// $_POST
$jsonString = $_POST['data'];
$newJsonString = json_encode($jsonString);
header('Content-Type: application/json');
echo $newJsonString;
否则,如果你想将JSON从JavaScript发送到PHP:
JavaScript:
var person = { name: "John" };
$.ajax({
contentType: "application/json", // php://input
//contentType: "application/x-www-form-urlencoded; charset=UTF-8", // $_POST
dataType : "json",
method: "POST",
url: "http://localhost/test/test.php",
data: person
})
.done(function(data) {
console.log("test: ", data);
$("#result").text(data.name);
})
.fail(function(data) {
console.log("error: ", data);
});
.PHP:
<?php
$jsonString = file_get_contents("php://input");
$phpObject = json_decode($jsonString);
$newJsonString = json_encode($phpObject);
header('Content-Type: application/json');
echo $newJsonString;
你可以尝试这样的事情:
var postData =
{
"bid":bid,
"location1":"1","quantity1":qty1,"price1":price1,
"location2":"2","quantity2":qty2,"price2":price2,
"location3":"3","quantity3":qty3,"price3":price3
}
$.ajax({
type: "POST",
dataType: "json",
url: "add_cart.php",
data: postData,
success: function(data){
alert('Items added');
},
error: function(e){
console.log(e.message);
}
});
JSON 编码应该自动发生,您的帖子转储应该为您提供类似的东西:
array(
"bid"=>bid,
"location1"=>"1",
"quantity1"=>qty1,
"price1"=>price1,
"location2"=>"2",
"quantity2"=>qty2,
"price2"=>price2,
"location3"=>"3",
"quantity3"=>qty3,
"price3"=>price3
)
只需删除:
...
//dataType: "json",
url: "index.php",
data: {myData:postData},
//contentType: "application/json; charset=utf-8",
...
您正在尝试发送带有 js 对象格式的 js 数组。
而不是使用
var a = new array();
a['something']=...
尝试:
var a = new Object();
a.something = ...
已经有一段时间了,但以防万一有人仍然需要它:
我需要传递的 JSON 对象:
0:{CommunityId: 509, ListingKey: "20281", Type: 10, Name: "", District: "", Description: "",…}
1:{CommunityId: 510, ListingKey: "20281", Type: 10, Name: "", District: "", Description: "",…}
Ajax 代码:
data: JSON.stringify(The-data-shows-above),
type: 'POST',
datatype: 'JSON',
contentType: "application/json; charset=utf-8"
而PHP方面:
json_decode(file_get_contents("php://input"));
它对我有用,希望它能有所帮助!