Abd*_*kib 30 php ajax jquery json
我想以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>
Run Code Online (Sandbox Code Playgroud)
在PHP中我使用:
if(isset($_POST['myData'])){
$obj = json_decode($_POST['myData']);
//some php operation
}
Run Code Online (Sandbox Code Playgroud)
当在php文件中添加print_r($ _ POST)时,它在firebug中显示array(0){}.
Roc*_*mat 41
失去了contentType: "application/json; charset=utf-8",.您没有向服务器发送JSON,而是发送正常的POST查询(恰好包含JSON字符串).
这应该使你的工作成功.
事情是,你根本不需要使用JSON.stringify或json_decode在这里.做就是了:
data: {myData:postData},
Run Code Online (Sandbox Code Playgroud)
然后在PHP中:
$obj = $_POST['myData'];
Run Code Online (Sandbox Code Playgroud)
Nie*_*sol 22
那是因为$_POST预先填充了表单数据.
要获取JSON数据(或任何原始输入),请使用php://input.
$json = json_decode(file_get_contents("php://input"));
Run Code Online (Sandbox Code Playgroud)
小智 11
使用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);
}
});
Run Code Online (Sandbox Code Playgroud)
在php中使用该对象:
$obj = json_decode($_POST["myData"]);
echo $obj->var;
Run Code Online (Sandbox Code Playgroud)
如果要通过$_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);
});
Run Code Online (Sandbox Code Playgroud)
PHP:
<?php
// $_POST
$jsonString = $_POST['data'];
$newJsonString = json_encode($jsonString);
header('Content-Type: application/json');
echo $newJsonString;
Run Code Online (Sandbox Code Playgroud)
否则,如果您要将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);
});
Run Code Online (Sandbox Code Playgroud)
PHP:
<?php
$jsonString = file_get_contents("php://input");
$phpObject = json_decode($jsonString);
$newJsonString = json_encode($phpObject);
header('Content-Type: application/json');
echo $newJsonString;
Run Code Online (Sandbox Code Playgroud)