Javascript Ajax多数组返回
目前,我尝试从ajax Post json请求中获取多数组响应。 经过几个小时的努力,我现在希望能在这里得到帮助 JS AJAX响应Javascript Ajax多数组返回,javascript,php,arrays,ajax,json,Javascript,Php,Arrays,Ajax,Json,目前,我尝试从ajax Post json请求中获取多数组响应。 经过几个小时的努力,我现在希望能在这里得到帮助 JS AJAX响应 var data = { "myid": "1234" }; $(".the-return").html(""); $.ajax({ type: "POST", dataType: "json", url: "../post_test/ajax.php", data: data, success: function(data) {
var data = {
"myid": "1234"
};
$(".the-return").html("");
$.ajax({
type: "POST",
dataType: "json",
url: "../post_test/ajax.php",
data: data,
success: function(data) {
$.each(data, function (i, item) {
$(".the-return").append("JSON: " + data["messages"]+"<br>");
});
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
})
return false;
所以我现在的问题是->如何将稳定的数组返回到Ajax成功函数
->以及如何在Sucess函数中读取返回值,以便使用
在PHP网站上
$items .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
$items.=如何操作的示例。Filevarex2.php返回JSON格式的数组。文件varex1.php解码JSON数据并将其显示在“警报”窗口中。要测试下一个代码,请创建两个文本文件,使用给定的名称,复制粘贴代码,打开浏览器并运行localhost/varex1.php
,如下所示:
varex2.php
<?php
// THIS IS AN ARRAY OF ARRAYS.
$products = Array( Array( "code" => "0401",
"name" => "shoes",
"price" => 700
),
Array( "code" => "0992",
"name" => "shirt",
"price" => 250
),
Array( "code" => "5800",
"name" => "glasses",
"price" => 400
)
);
$json = json_encode( $products );
echo $json; // ARRAY RETURNED IN JSON FORMAT.
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : { },
url : 'varex2.php',
success: function ( data ) {
var i;
var obj = JSON.parse( data ); // DECODE JSON DATA.
for ( i = 0; i < obj.length; i++ ) // WALK THE SUB-OBJECTS.
alert( obj[ i ].code + "\n" +
obj[ i ].name + "\n" +
obj[ i ].price );
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
</head>
<body>
<button onclick="myAjax()">Click here to get the data</button>
</body>
</html>
varex1.php
<?php
// THIS IS AN ARRAY OF ARRAYS.
$products = Array( Array( "code" => "0401",
"name" => "shoes",
"price" => 700
),
Array( "code" => "0992",
"name" => "shirt",
"price" => 250
),
Array( "code" => "5800",
"name" => "glasses",
"price" => 400
)
);
$json = json_encode( $products );
echo $json; // ARRAY RETURNED IN JSON FORMAT.
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : { },
url : 'varex2.php',
success: function ( data ) {
var i;
var obj = JSON.parse( data ); // DECODE JSON DATA.
for ( i = 0; i < obj.length; i++ ) // WALK THE SUB-OBJECTS.
alert( obj[ i ].code + "\n" +
obj[ i ].name + "\n" +
obj[ i ].price );
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
</head>
<body>
<button onclick="myAjax()">Click here to get the data</button>
</body>
</html>
函数myAjax(){
$.ajax({type:'POST',
数据:{},
url:'varex2.php',
成功:功能(数据){
var i;
var obj=JSON.parse(data);//解码JSON数据。
对于(i=0;i
你能再给我解释一下吗?也许用一个代码示例来理解?如果使用JSON_encode()函数将其编码为JSON,那就太棒了。。。我忘了在代码中添加它了,对不起。我需要类似于for-each循环的东西,比如for-each-messages-in-messages-like-for-each-messages-in-messages-alert('get messages from…-to…,says:asdsd');
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : { },
url : 'varex2.php',
success: function ( data ) {
var i;
var obj = JSON.parse( data ); // DECODE JSON DATA.
for ( i = 0; i < obj.length; i++ ) // WALK THE SUB-OBJECTS.
alert( obj[ i ].code + "\n" +
obj[ i ].name + "\n" +
obj[ i ].price );
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
</head>
<body>
<button onclick="myAjax()">Click here to get the data</button>
</body>
</html>