与JSON响应,如何jQuery的Ajax请求? [英] jQuery ajax request with json response, how to?

查看:111
本文介绍了与JSON响应,如何jQuery的Ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送带有2后值的Ajax请求,第一个是动作至极定义什么样的行动我的PHP脚本必须解析,另一种是ID至极是用户的ID它来解析脚本对于。
服务器返回6值的数组中()和随后的连接coded到JSON的PHP函数: json_en code();
我的一些反应是HTML,但是当我连接code到JSON,它避开了/,使其成为\ /
我如何禁用?

还当我不知道如何在jQuery中显示这个当我得到的服务器响应,我只是想,把它全部变成一个div只会显示C $ CS我已请求的数量和HTML $,但它显示阵列因为它是连接codeD在PHP。
PHP:
$响应=阵列();
$回应[] =< A HREF =''>链接< / A>中;
$回应[] = 1;
回声json_en code($响应)
jQuery的:
$。阿贾克斯({
键入:POST,数据类型:JSON,网址:main.php,数据行动= LOADALL和ID =+ ID,
完成:功能(数据){
$('#主)HTML(data.responseText);
}
});
如何使这工作JSON?

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 is 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.
php:
$response = array();
$response[] = "<a href=''>link</a>";
$response[] = 1;
echo json_encode($response)
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 ?

推荐答案

您需要调用

$.parseJSON();

例如:

...
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);
    } ...

看到这 http://api.jquery.com/jQuery.parseJSON/

如果你仍然有斜杠的问题:  搜索 security.magicquotes.disabling.php 要么: function.stripslashes.php

if you still have the problem of slashes: search for security.magicquotes.disabling.php or: function.stripslashes.php

注意:

下面这个答案是为那些谁尝试使用 $。阿贾克斯的dataType 属性设置为 JSON ,甚至是得到了错误的反应类型。定义标题(内容类型:应用程序/ JSON'); 中的服务器可以解决问题,但如果你正在返回的text / html 或任何其他类型的 $。阿贾克斯方法应该将其转换为 JSON 。我做一个测试与旧版本的jQuery和唯一版本后 1.4.4 $。阿贾克斯力将任何内容类型的的dataType 通过。所以,如果你有这个问题,尝试更新您的jQuery的版本。

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.

这篇关于与JSON响应,如何jQuery的Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆