php 带有json响应的jQuery ajax请求,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9098649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax request with json response, how to?
提问by Flaashing
I am sending a ajax request with 2 post values, the first is "action" wich defines what actions my php script has to parse, the other is "id" wich is the id of the user it has to parse the script for.
The server returns 6 values inside a array() and is then encoded to JSON with the PHP function: json_encode();
Some of my responses are HTML, but when I encode it to JSON, it escapes "/"
so it becomes "\/"
how do I disable that?
also when I don't know how to display this in jQuery when I get the server response, I just thought that putting it all into a div would just display the numbers and HTML codes I had requested, but it displays the array as it is encoded in PHP.
我正在发送一个带有 2 个 post 值的 ajax 请求,第一个是“action”,它定义了我的 php 脚本必须解析的动作,另一个是“id”,它必须解析脚本的用户的 ID。
服务器在 array() 中返回 6 个值,然后使用 PHP 函数将其编码为 JSON:json_encode();
我的一些响应是 HTML,但是当我将其编码为 JSON 时,它会转义,"/"
因此"\/"
我该如何禁用它?
此外,当我在收到服务器响应时不知道如何在 jQuery 中显示它时,我只是认为将其全部放入 div 中只会显示我请求的数字和 HTML 代码,但它会按原样显示数组用 PHP 编码。
PHP
PHP
$response = array();
$response[] = "<a href=''>link</a>";
$response[] = 1;
echo json_encode($response);
jQuery:
jQuery:
$.ajax({
type: "POST",
dataType: "json",
url: "main.php",
data: "action=loadall&id=" + id,
complete: function(data) {
$('#main').html(data.responseText);
}
});
how do I make this to working JSON?
我如何使这个工作 JSON?
回答by Guilherme
You need to call the
您需要致电
$.parseJSON();
For example:
例如:
...
success: function(data){
var json = $.parseJSON(data); // create an object with the key of the array
alert(json.html); // where html is the key of array that you want, $response['html'] = "<a>something..</a>";
},
error: function(data){
var json = $.parseJSON(data);
alert(json.error);
} ...
see this in http://api.jquery.com/jQuery.parseJSON/
在http://api.jquery.com/jQuery.parseJSON/ 中看到这个
if you still have the problem of slashes: search for security.magicquotes.disabling.phpor: function.stripslashes.php
如果还有斜线问题:搜索security.magicquotes.disabling.php或: function.stripslashes.php
Note:
笔记:
This answer here is for those who try to use $.ajax
with the dataType
property set to json
and even that got the wrong response type. Defining the header('Content-type: application/json');
in the server may correct the problem, but if you are returning text/html
or any other type, the $.ajax
method should convert it to json
. I make a test with older versions of jQuery and only after version 1.4.4
the $.ajax
force to convert any content-type to the dataType
passed. So if you have this problem, try to update your jQuery version.
这里的答案适用于那些尝试$.ajax
将dataType
属性设置为json
甚至得到错误响应类型的人。header('Content-type: application/json');
在服务器中定义可能会纠正问题,但如果您正在返回text/html
或任何其他类型,该$.ajax
方法应将其转换为json
. 我做一个测试与旧版本的jQuery和唯一版本后1.4.4
的$.ajax
力的任何内容类型转换为dataType
通过。因此,如果您遇到此问题,请尝试更新您的 jQuery 版本。
回答by sgb
Firstly, it will help if you set the headers of your PHP to serve JSON:
首先,如果您将 PHP 的标头设置为提供 JSON 会有所帮助:
header('Content-type: application/json');
Secondly, it will help to adjust your ajax call:
其次,这将有助于调整您的 ajax 调用:
$.ajax({
url: "main.php",
type: "POST",
dataType: "json",
data: {"action": "loadall", "id": id},
success: function(data){
console.log(data);
},
error: function(error){
console.log("Error:");
console.log(error);
}
});
If successful, the response you receieve should be picked up as true JSON and an object should be logged to console.
如果成功,您收到的响应应该被选择为真正的 JSON,并且一个对象应该被记录到控制台。
NOTE: If you want to pick up pure html, you might want to consider using another method to JSON, but I personally recommend using JSON and rendering it into html using templates (such as Handlebars js).
注意:如果您想获取纯 html,您可能需要考虑使用另一种 JSON 方法,但我个人建议使用 JSON 并使用模板(例如Handlebars js)将其渲染为 html 。
回答by ArtNazarov
Connect your javascript clientside controller and php serverside controller using sending and receiving opcodes with binded data. So your php code can send as response functional delta for js recepient/listener
使用带有绑定数据的发送和接收操作码连接您的 javascript 客户端控制器和 php 服务器端控制器。因此,您的 php 代码可以作为 js 接收方/侦听器的响应功能增量发送
see https://github.com/ArtNazarov/LazyJs
见https://github.com/ArtNazarov/LazyJs
Sorry for my bad English
对不起,我的英语不好
回答by ShankarSangoli
Since you are creating a markup as a string you don't have convert it into json. Just send it as it is combining all the array elements using implode
method. Try this.
由于您将标记创建为字符串,因此您无需将其转换为 json。只需发送它,因为它使用implode
方法组合所有数组元素。尝试这个。
PHP change
PHP更改
$response = array();
$response[] = "<a href=''>link</a>";
$response[] = 1;
echo implode("", $response);//<-----Combine array items into single string
JS (Change the dataType from json to html or just don't set it jQuery will figure it out)
JS(将 dataType 从 json 更改为 html 或者只是不设置它 jQuery 会弄清楚的)
$.ajax({
type: "POST",
dataType: "html",
url: "main.php",
data: "action=loadall&id=" + id,
success: function(response){
$('#main').html(response);
}
});
回答by pratik Bharodiya
Try below code. Json parse() method is not required since your datatype is json itself. Parse is needed to convert text into a JavaScript object.
试试下面的代码。不需要 Json parse() 方法,因为您的数据类型是 json 本身。Parse 需要将文本转换为 JavaScript 对象。
$.ajax({
url : base_url+"Login/submit",
type: "POST",
dataType: "json",
data : {
'username': username,
'password': password
},
success: function(data)
{
alert(data.status);
}
});