admin管理员组文章数量:1332881
I can't for the life of me figure out what I'm doing wrong. It seems like it should be simple because I can't find anyone else with this issue but I can't figure out to send basic data via javascript(jQuery) to PHP and decode it. For the sake of simplicity, this is what I have:
JAVASCRIPT
var json_data = { "name" : "john doe" };
$.ajax({
type: "POST",
url: "../bin/process.php",
dataType: "json",
data: json_data
});
and my PHP FILE
$arr = json_decode("json_data", true);
$fp = fopen('data.txt', "w");
fwrite($fp, $arr['name']);
fclose($fp);
The file I'm writing ends up with nothing in it. If I do an:
fwrite($fp, 'test');
I get a file with the word test in it but no matter what I do I don't get the json data I sent.
Can someone please share a thorough example of A to Z. Thanks for any help.
I can't for the life of me figure out what I'm doing wrong. It seems like it should be simple because I can't find anyone else with this issue but I can't figure out to send basic data via javascript(jQuery) to PHP and decode it. For the sake of simplicity, this is what I have:
JAVASCRIPT
var json_data = { "name" : "john doe" };
$.ajax({
type: "POST",
url: "../bin/process.php",
dataType: "json",
data: json_data
});
and my PHP FILE
$arr = json_decode("json_data", true);
$fp = fopen('data.txt', "w");
fwrite($fp, $arr['name']);
fclose($fp);
The file I'm writing ends up with nothing in it. If I do an:
fwrite($fp, 'test');
I get a file with the word test in it but no matter what I do I don't get the json data I sent.
Can someone please share a thorough example of A to Z. Thanks for any help.
Share Improve this question asked May 9, 2010 at 21:42 dscherdscher 1,5452 gold badges16 silver badges23 bronze badges2 Answers
Reset to default 5The ajax request that you make with jQuery will send the parameter 'name', with the value 'john doe', and not the whole object. If you want to send the whole object, you have to pass it like this:
data: { parameters: json_data }
On the PHP side, you can get the variables from the $_POST superglobal. Using your example, you would use:
$name = $_POST['name'];
Or, if you send the whole object, using my example:
$params = $_POST['parameters'];
There is no need to use json_decode(), since the parameters you pull out from the $_POST array will be native PHP variables already.
You only need to use it, if you have a json string, which you want to turn into a PHP variable, which is not the case here, since jQuery will "transform" the javascript object, to a query string in the background.
It is a rare case that you need to send data from javascript in a JSON form, but if you want to do that you need something like:
// JS
var person = "{ name: 'John Doe' }"; // notice the double quotes here, this is a string, and not an object
$.ajax({
type: "POST",
url: "../bin/process.php",
dataType: "json",
data: { json: person }
});
// PHP
$json = $_POST['json']; // $json is a string
$person = json_decode($json); // $person is an array with a key 'name'
jQuery cannot encode data to JSON, only decode it (the poorly named dataType
parameter actually refers to the type of the reponse). Use json2.js for encoding.
本文标签: javascriptSending JSON(jQuery) to PHP and decoding itStack Overflow
版权声明:本文标题:javascript - Sending JSON(jQuery) to PHP and decoding it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742311478a2450957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论