我是JSON和ajax的新手。基本上,我正在尝试从mysql数据库中获取事件列表,并将它们作为JSON编码文件发送回.ajax()并从该函数中显示这些事件列表。我尝试了不同的方法,但我认为我没有走在正确的轨道上。作为Ajax和JSON的新手,使用ajax发送和接收数据有点令人困惑,但很有趣
JSON 注释和数组让我感到困惑,以至于我不知道如何访问内部元素就被惊呆了
$.ajax({
...
...
...
})
.done(function(result)
{
})
result
是对象、数组还是字符串? 我应该在这里使用JSON.parse()
方法,还是应该直接使用result
来处理和显示数据?
这是我的后端 php 文件的输入格式,
{"data":
{
"dept": "CSE"
}
}
此输入来自下拉列表,
$("#dept_drop_down").on("change", function() {
...
...
})
我的php文件的输出格式是,
{
"data": {
"status": "success",
"response_code": "202",
"events": {
"1": {
"name": "Help Dexter",
"desc": "Help Dexter to Solve the Puzzle",
"coordinate": "1307",
"dept": "CSE"
},
"2": {
"name": "Code Hunt",
"desc": "Lets hunt the CODE ..!!",
"coordinate": "2145",
"dept": "CSE"
}
}
}
}
请帮助我使用JavaScript代码,用于发送输入格式JSON和接收输出格式JSON并使用AJAX(上面给出的输入和输出格式)显示它们。
等待您的帮助。提前感谢...
这是我的代码...
$(document).ready(function(){
$("#dept_drop_down").on("change", function(){
var dat = $(document.getElementById("dept")).serializeJSON();
var postdata = JSON.stringify(dat);
$.ajax({
url: "elist.php",
type: "POST",
data: postdata,
datatype: 'application/json',
error: function(xhr,a,b){alert("This is "+xhr.status)},
beforeSend: function(){alert("Sending.......")},
success:function(result){
var obj=result;
d=$.parseJSON(result);
if(obj.data.resopnse_code==202)
{
//object processing .. Here is the place i need help
}
else if(obj.data.response_code==200)
{
//object processing .. Here is the place i need help
}
else if(obj.data.response_code==201)
{
//object processing .. Here is the place i need help
}
else if(obj.data.response_code==400)
{
//object processing .. Here is the place i need help
}
}
});
});
});
结果是一个字符串。你可以用jQuery.parseJSON创建一个JSON对象。
var jsonObj = jQuery.parseJSON( result );
您可以使用您创建的变量(在我的示例 jsonObj 中)和元素名称访问内部元素。
假设你想要 JSON 中第二个事件的坐标,你可以使用
jsonObj.events.data[1].coordinate
那是:
- jsonObj:访问主 JSON 对象
- data:访问 jsonObj 中的数据元素 事件
- :访问数据中的事件 1
- :访问事件元素中的 1 元素 坐标
- :第二个事件元素中的访问坐标
这将返回字符串"1307"
总的来说,当你得到它时,Ajax并不难,你似乎已经很好地掌握了它。
这里有一个游乐场:http://codepen.io/anon/pen/WrZQrj
这完全取决于您的PHP脚本作为内容类型返回调用的内容。例如,假设我们有一个 json.json 文件,其中包含上面的对象文本,我可以像这样编写 PHP 脚本:
<?php
$content =file_get_contents ('json.json') ;
echo $content ; // I echo a string here
die ;
Ajax 调用将收到一个文本字符串,因为该调用默认为文本/纯内容类型
$.post ('php.php', {}, function (ret, status) {
alert (ret) ; // Will alert text
if ( typeof ret === 'string' )
ret =JSON.parse (ret) ;
alert (ret) ; // Will alert [Object] [Object]
// ret.data.status == 'success'
}) ;
现在,如果我的 PHP 脚本执行以下操作:
<?php
$content =file_get_contents ('json.json') ;
header ('Content-Type: application/json') ;
echo $content ; // I echo a string here still
die ;
这是不同的。现在我得到了一个 JSON 对象:
$.post ('php.php', {}, function (ret, status) {
alert (ret) ; // Will alert [Object] [Object]
if ( typeof ret === 'string' ) // << will not execute anymore
ret =JSON.parse (ret) ;
alert (ret) ; // Will alert [Object] [Object]
// ret.data.status == 'success'
}) ;
我在第二个中保留了 if() 和 JSON.parse(),但在最后一种情况下不再使用它们。
最后一件事,在PHP代码中,我回显了一个来自文件的字符串,但是如果你得到了一个由脚本构建的对象,这里是代码
$content =(object)[] ;
$content.data =(object)[ "status" => "success" } ;
...
header ('Content-Type: application/json') ;
echo json_encode ($content) ; // I echo a string here
die ;