Javascript Ajax无法加载php json
问题是,json文件没有显示。Im使用Xampp作为本地服务器 php文件:Javascript Ajax无法加载php json,javascript,php,jquery,json,sjax,Javascript,Php,Jquery,Json,Sjax,问题是,json文件没有显示。Im使用Xampp作为本地服务器 php文件: $array = array( array( "title" => "Erster Eintrag", "description" => "Description", "link" => "http://", "pubDate" => "02.07.2015" ), array( "title" => "Zweiter Eintrag",
$array = array(
array(
"title" => "Erster Eintrag",
"description" => "Description",
"link" => "http://",
"pubDate" => "02.07.2015"
),
array(
"title" => "Zweiter Eintrag",
"description" => "Description",
"link" => "http://",
"pubDate" => "02.07.2015"
)
);
echo json_encode($json);
html文件:
$ajax({
url:'localhost/uebung/staticfeed.php',
type:'POST',
data:'data',
dataType: 'json',
success: function(result){
$('#feeds').html(result[0]);
}
})
在PHP中使用
返回包含值的JSON表示形式的字符串
使用$array
来json\u编码
,而不是$json
echo json_encode($array);
/// ^^^^^^^^^
在PHP中使用
返回包含值的JSON表示形式的字符串
使用$array
来json\u编码
,而不是$json
echo json_encode($array);
/// ^^^^^^^^^
JavaScript使用$.ajax,然后使用完整URL
$.ajax({
url:'http://localhost/uebung/staticfeed.php',
type:'POST',
data:'data',
dataType: 'json',
success: function(result){
$('#feeds').html(result[0]);
});
您还需要在php文件中对数组进行编码
echo json_encode($array);
JavaScript使用$.ajax,然后使用完整URL
$.ajax({
url:'http://localhost/uebung/staticfeed.php',
type:'POST',
data:'data',
dataType: 'json',
success: function(result){
$('#feeds').html(result[0]);
});
您还需要在php文件中对数组进行编码
echo json_encode($array);
将
$ajax
更改为$。ajax
,这将删除代码中的错误,其他一切正常将$ajax
更改为$。ajax
,这将删除代码中的错误,其他一切正常您的PHP答案中缺少JSON标题,Jquery可能会将您的响应解释为纯文本或HTML
另外,您正在回显$json
,并且您的数组是$array
在PHP中尝试以下方法:
header('Content-type: application/json');
$array = [["title" => "Erster Eintrag","description" => "Description","link" => "http://","pubDate" => "02.07.2015"],["title" => "Zweiter Eintrag","description" => "Description","link" => "http://","pubDate" => "02.07.2015"]];
echo json_encode($array);
在您的HTML上:
$.ajax({type: "POST", url: "localhost/uebung/staticfeed.php", data:data, dataType: "json", timeout: 25000, success: function (result) {
$('#feeds').html('First array:' + result.[0].title + '<br />Seccond array:' + result.[1].title );
}});
$.ajax({type:“POST”,url:“localhost/uebung/staticfeed.php”,数据:数据,数据类型:“json”,超时:25000,成功:函数(结果){
$('#feeds').html('第一个数组:'+result[0].title+'
第二个数组:'+result[1].title);
}});
您需要在[result]内的数组中选择值…如果PHP答案中缺少JSON标题,Jquery可能会将您的响应解释为纯文本或HTML 另外,您正在回显
$json
,并且您的数组是$array
在PHP中尝试以下方法:
header('Content-type: application/json');
$array = [["title" => "Erster Eintrag","description" => "Description","link" => "http://","pubDate" => "02.07.2015"],["title" => "Zweiter Eintrag","description" => "Description","link" => "http://","pubDate" => "02.07.2015"]];
echo json_encode($array);
在您的HTML上:
$.ajax({type: "POST", url: "localhost/uebung/staticfeed.php", data:data, dataType: "json", timeout: 25000, success: function (result) {
$('#feeds').html('First array:' + result.[0].title + '<br />Seccond array:' + result.[1].title );
}});
$.ajax({type:“POST”,url:“localhost/uebung/staticfeed.php”,数据:数据,数据类型:“json”,超时:25000,成功:函数(结果){
$('#feeds').html('第一个数组:'+result[0].title+'
第二个数组:'+result[1].title);
}});
您需要在[result]内的数组中选择值……在回显之前是否对php进行json_编码,或者您正在做什么?加载
localhost/uebung/staticfeed.php
时会发生什么?如果没有,则是您的服务器配置。如果返回数组,则需要json\u encode()
数据,并使用json\u encode()将头设置为json type.im。使用localhost/uebung/staticfeed.php,我可以完美地显示json。我想@Lylo可能会有正确的答案。你是在回音之前对php进行json编码还是在做什么?加载localhost/uebung/staticfeed.php
时会发生什么?如果没有,则是您的服务器配置。如果返回数组,则需要json\u encode()
数据,并使用json\u encode()将头设置为json type.im。使用localhost/uebung/staticfeed.php,我可以完美地显示json。我想@Lylo可能会有正确的答案。我忘了写了。@FreshManu检查更新的答案。请注意,使用$array来代替$json进行json编码。
我忘记了,只是忘了写了。@FreshManu检查更新的答案。注意使用$array来进行json编码而不是$json。
在这种情况下,由于$.ajax()
调用显式地列出了数据类型:“json”
,因此jQuery不会将其解释为json以外的内容,但我给出+1,因为OP应该提供您提到的适当的内容类型头。这只是一个最佳实践。事情是。。。当我们调用json文件时,jQuery可以将答案解释为任何其他错误,因为“不是”json。另一方面,如果我们不在ajax调用中定义JSON,那么jQuery将以最好的方式处理该响应,而不是将其发送到error。谢谢。在这种情况下,由于$.ajax()
调用显式列出了数据类型:“json”
,因此jQuery不会将其解释为json以外的内容,但我给出+1,因为OP应该提供您提到的适当的内容类型头。这只是一个最佳实践。事情是。。。当我们调用json文件时,jQuery可以将答案解释为任何其他错误,因为“不是”json。另一方面,如果我们不在ajax调用中定义JSON,那么jQuery将以最好的方式处理该响应,而不是将其发送到error。谢谢