jquery ajax response json,jQuery ajax request with json response, how to?

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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.

PHP

$response = array();

$response[] = "link";

$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?

回答1:

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'] = "something..";

},

error: function(data){

var json = $.parseJSON(data);

alert(json.error);

} ...

see this in

http://api.jquery.com/jQuery.parseJSON/

if you still have the problem of slashes:

search for security.magicquotes.disabling.php

or:

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.

回答2:

Firstly, it will help if you set the headers of your PHP to serve JSON:

header('Content-type: application/json');

Secondly, it will help to adjust your ajax call:

$.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.

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

回答3:

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

see https://github.com/ArtNazarov/LazyJs

Sorry for my bad English

回答4:

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.

PHP change

$response = array();

$response[] = "link";

$response[] = 1;

echo implode("", $response);//

JS (Change the dataType from json to html or just don't set it jQuery will figure it out)

$.ajax({

type: "POST",

dataType: "html",

url: "main.php",

data: "action=loadall&id=" + id,

success: function(response){

$('#main').html(response);

}

});

回答5:

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.

$.ajax({

url : base_url+"Login/submit",

type: "POST",

dataType: "json",

data : {

'username': username,

'password': password

},

success: function(data)

{

alert(data.status);

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值