在调用中使用返回的json值不起作用
using returned json values in calls not working
试图使用我从php返回的值,但这些值无法正常工作。
<script type="text/javascript">
$(function () {
$('#delete').on('click', function () {
var $form = $(this).closest('form');
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
dataType : 'json'
}).done(function (response) {
if (response.success == 'success') {
// fade out the deleted comp
$("#c"+response.computer_id+"").fadeOut("slow");
// remove from the drop down
$("#comp_selection option[value='#c"+response.computer_id+"']").remove();
} else {
alert('Some error occurred.');
}
});
});
});
</script>
我从php文件返回:
echo json_encode($ajax_result);
正在返回:
{"computer_id":1,"success":"success"}
编辑:我在代码中发现了一个小错误,我返回了一个与预期不同的值。一切都很好,上述内容一开始确实是正确的。
您应该使用firebug或浏览器中的任何开发工具进行调试-firebug在Firefox中非常适用,因为您可以看到AJAX调用返回的JSON数据。
也就是说,你的问题是数据可能正在响应中。d-所以看看response.d.success和response.d.computer_id
也许您从PHP服务器接收的内容是以简单文本的形式发送的,并且必须使用为JSON内容设置的标头发送:
header('Content-Type: application/json');
相关文章:
- ngResource没有'从JSON文件解析HTML时不起作用
- 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数据不起作用
- wordpress phonegap json不起作用
- DatePicker php json 不起作用
- 每个 JSON 不起作用
- 带有 Angular JS 的 JSON 不起作用(Ionic & Cordova)
- 访问json不起作用
- AJAX调用中返回的JSON不起作用
- jQuery's从HTML解析JSON不起作用
- 使用jquery解析json不起作用