I'm trying to write a jQuery function to send a query string to a PHP script, but I can't seem to get the text to get to the server in the correct format. I want to send this query string (with the appropriate URL encoding):
我正在尝试编写一个jQuery函数向PHP脚本发送查询字符串,但是我似乎无法以正确的格式将文本发送到服务器。我想发送这个查询字符串(使用适当的URL编码):
data={"name":"Chris"}
where 'data' is always a JSON string. Using jQuery's .ajax
function I tried setting my data variable to
这里的“数据”总是一个JSON字符串。使用jQuery的.ajax函数,我尝试将数据变量设置为
data: { 'data': {"name":"chris"} },
but PHP ends up getting:
但是PHP最终会:
data[name]=chris
What's the proper way to send the data back to the server so that the JSON string is properly reserved, without having to hand-craft the string?
将数据发送回服务器以使JSON字符串得到适当保留的正确方法是什么?
4 个解决方案
#1
2
First, you'll need to use json2.js because jQuery does not include the capability to output JSON, only to parse it, and the method we will be using is not supported in IE 6/7. Convert your JavaScript object to JSON:
首先,您需要使用json2。因为jQuery不包含输出JSON的能力,只需要解析它,而我们将使用的方法在IE 6/7中不受支持。将JavaScript对象转换为JSON:
var encoded = JSON.stringify(data);
Then you need to include that JSON-formatted string as request data:
然后需要将json格式的字符串包含为请求数据:
$.getJSON(url, {data: encoded}, function() { ... });
Edit: An older version of this post referred to the jquery-json plugin, but it's obvious that that plug-in was written when jQuery 1.3.x was current.
编辑:这篇文章的一个旧版本引用了jQuery -json插件,但是很明显这个插件是在jQuery 1.3时编写的。x是电流。
#2
1
All you have to do is put quotes around the string
你所要做的就是在字符串周围加上引号。
data: { 'data': '{"name":"chris"}' }
#3
0
Although, unclear from your question, if you are trying to know how to handle JSON strings properly in PHP, the best way would be to use the functions json_encode
and json_decode
.
虽然,不清楚您的问题,如果您试图了解如何在PHP中正确地处理JSON字符串,最好的方法是使用json_encode和json_decode函数。
#4
0
This is wrong:
这是错误的:
data: { 'data': {"name":"chris"} },
You get a indexed array with key of name and value of chris.
您将得到一个具有chris的名称和值的键的索引数组。
this is right:
这是正确的:
{ name : "chris" }
If you want in php:
如果你想用php:
name = "chris";
Then you must send
那么你必须发送
{ name : "chris" }
Depending on if you GET you can get:
取决于你能否得到:
$name = $_GET["name"];
echo $name; // chris
#1
2
First, you'll need to use json2.js because jQuery does not include the capability to output JSON, only to parse it, and the method we will be using is not supported in IE 6/7. Convert your JavaScript object to JSON:
首先,您需要使用json2。因为jQuery不包含输出JSON的能力,只需要解析它,而我们将使用的方法在IE 6/7中不受支持。将JavaScript对象转换为JSON:
var encoded = JSON.stringify(data);
Then you need to include that JSON-formatted string as request data:
然后需要将json格式的字符串包含为请求数据:
$.getJSON(url, {data: encoded}, function() { ... });
Edit: An older version of this post referred to the jquery-json plugin, but it's obvious that that plug-in was written when jQuery 1.3.x was current.
编辑:这篇文章的一个旧版本引用了jQuery -json插件,但是很明显这个插件是在jQuery 1.3时编写的。x是电流。
#2
1
All you have to do is put quotes around the string
你所要做的就是在字符串周围加上引号。
data: { 'data': '{"name":"chris"}' }
#3
0
Although, unclear from your question, if you are trying to know how to handle JSON strings properly in PHP, the best way would be to use the functions json_encode
and json_decode
.
虽然,不清楚您的问题,如果您试图了解如何在PHP中正确地处理JSON字符串,最好的方法是使用json_encode和json_decode函数。
#4
0
This is wrong:
这是错误的:
data: { 'data': {"name":"chris"} },
You get a indexed array with key of name and value of chris.
您将得到一个具有chris的名称和值的键的索引数组。
this is right:
这是正确的:
{ name : "chris" }
If you want in php:
如果你想用php:
name = "chris";
Then you must send
那么你必须发送
{ name : "chris" }
Depending on if you GET you can get:
取决于你能否得到:
$name = $_GET["name"];
echo $name; // chris