ajax get-json函数没有'不起作用
ajax get json function doesn't work
我想从php页面获得一个json文件,但我的代码不起作用,怎么了?
我的php页面是
header('Content-type: application/json');
$jsonstart="{'files' : [";
$jsonend="]}";
$content="{'firstname' : '".$_GET['name']."' , 'lastname' : 'izadi'}";
$jsonfile=$jsonstart.$content.$jsonend;
print $jsonfile;
我的ajax代码是
$(document).ready(function(){
$.getJSON("getfilesinfo.php?name=afshin", function(data){
alert();
var test=JSON.parse(data);
alert("Data: " + test.files[1].firstname + "'nStatus: " + status);
});
});
您必须像这样在php文件中使用json_encode()。
<?php
header('Content-type: application/json');
$array = array(
'firstname' => $_GET['name'],
'lastname' => 'izadi'
);
echo json_encode($array);
exit;
使用json_encode 尝试这个更干净的代码
<?php
header('Content-type: application/json');
$array = array(
'firstname' => $_GET['name'],
'lastname' => 'izadi'
);
echo json_encode(array('files'=>array($array)));
js:
$(document).ready(function(){
$.getJSON("getfilesinfo.php?name=afshin", function(test){
alert("Data: " + test.files[1].firstname );
});
});
相关文章:
- ngResource没有'从JSON文件解析HTML时不起作用
- getDocument by id/get input text from button dons'不起作用
- Backbone.js 不起作用,而 Underscore、JSON 和 jQuery 可以
- 想要将 JSON 字符串传递给 javascript 变量 [ JSON.parse(),eval()] 对我不起作用,
- 显示 JSON 将不起作用
- Ajax 对 JSON 的请求不起作用
- JSON 数据解析不起作用
- 当图像和链接是 json 对象属性的值时,它们不起作用
- 尝试使用 AngularJS ng-repeat 显示 JSON 数据不起作用
- 对 URL 的 JSON-P 请求不起作用
- ajax get-json函数没有'不起作用
- 将JSON对象转换为敲除observable在IE8中不起作用,但在所有其他浏览器中都起作用
- 从d3.js中的文件读取json数据不起作用
- JSON.parse()在pentaho中的javascript中不起作用
- 在调用中使用返回的json值不起作用
- angularjs不起作用的Json响应
- wordpress phonegap json不起作用
- SAPUI5绑定问题:json文件中维护的列表项链接不起作用
- 反伪造令牌和Ajax JSON.stringify Post不起作用
- 由于选择器不起作用,json中包含日期的Meteor db.find