admin管理员组文章数量:1389779
I'm trying to send my javascript object to PHP via JSON.stringify()
Javascript:
$('#save').on('click touch', function(){
obj = {
"1" : {
"1" : "hey",
"2" : "hay"
},
"2" : {
"1" : "hey",
"2" : "hay"
}
}
var json = JSON.stringify( obj );
console.log(json)
$.ajax({
type: 'POST',
url: 'ajax.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
ajax.php:
<?php
$obj = json_decode($json);
echo $obj;
?>
But this code returns an error saying that $json
is not defined.
I have no idea why this is not working.
I'm trying to send my javascript object to PHP via JSON.stringify()
Javascript:
$('#save').on('click touch', function(){
obj = {
"1" : {
"1" : "hey",
"2" : "hay"
},
"2" : {
"1" : "hey",
"2" : "hay"
}
}
var json = JSON.stringify( obj );
console.log(json)
$.ajax({
type: 'POST',
url: 'ajax.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
ajax.php:
<?php
$obj = json_decode($json);
echo $obj;
?>
But this code returns an error saying that $json
is not defined.
I have no idea why this is not working.
-
2
You have not set the
data
attribute on$.ajax
options. – Henrique Barcelos Commented Jan 13, 2016 at 20:39 -
POST data will be available in the
$_POST
variable in PHP – csum Commented Jan 13, 2016 at 20:42 -
@csum: only if OP actually bothers to tell jquery to USE that
json
var in the ajax call... – Marc B Commented Jan 13, 2016 at 20:46 -
@MarcB yup, true. It was more of a general statement. POST data is in
$_POST
– csum Commented Jan 13, 2016 at 21:05 - I have fixed the issue. Thank you all – Joost Hobma Commented Jan 13, 2016 at 21:27
5 Answers
Reset to default 6There are 2 problems.
- You are not sending any data with the request
- That's not the way you'll get the value from a request in PHP
First, add this*:
$.ajax({
type: 'POST',
url: 'ajax.php',
data : { json: json }, // <---------------------
...
* this works just because jQuery implementation will automatically convert any non-string data argument into a form-urlencoded query string. See the docs.
Then, in your PHP, you should do:
$jsonStr = $_POST['json'];
$json = json_decode($jsonStr);
Edit:
Another possible way:
$.ajax({
type: 'POST',
url: 'ajax.php',
data : json , // <---------------------
...
This way, your data will not be a valid form-urlencoded
input, so PHP will not parse it into $_POST
, but you still can get the contents of your input doing this:
$jsonStr = file_get_contents("php://input");
$json = json_decode($jsonStr);
Well - you never pass your data in the AJAX request!
$.ajax({
type: 'POST',
url: 'ajax.php',
data: json //<---- RIGHT HERE
success: function(data) {
alert(data);
$("p").text(data);
}
});
You have to send the obj with the ajax request
$.ajax({
type: 'POST',
url: 'ajax.php',
data : json,
dataType : 'json' // for json response
...
Check here for reference jQuery ajax
Data parameter: Specifies data to be sent to the server.
Try this:
$('#save').on('click touch', function(){
obj = {
"1" : {
"1" : "hey",
"2" : "hay"
},
"2" : {
"1" : "hey",
"2" : "hay"
}
}
var json = JSON.stringify( obj );
$.ajax({
data : json,
type: 'POST',
url: 'ajax.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
Replace your ajax code with this.
$.ajax({
type: 'POST',
url: 'ajax.php',
data: json
success: function(data) {
alert(data);
$("p").text(data);
}
});
For ajax php
<?php
$obj = json_decode($_POST['data']);
echo $obj;
?>
本文标签: passing JavaScript object to PHP not workingStack Overflow
版权声明:本文标题:passing JavaScript object to PHP not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744585487a2614170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论