在下面的函数中jquery ajax get调用应该将一些参数传递给test.php,让test.php根据这些参数执行一些测试,并将(jason编码的)测试结果(status_general)返回到.get()。 但是,下面代码中的console.log调用显示未定义的值,而我的php调试器显示test.php返回正确的jason编码测试结果。 我的.get代码基于http://api.jquery.com/jQuery.get的示例5。
有什么问题的建议/如何修复它? 先谢谢您的帮助。
码:
$.get( "test.php", { command: "test", val1: ipaddress, val2: "" }) .done(function(data) { var test_result = data.status_general; console.log('test result: '+test_result); },"json");The jquery ajax get call in the function below is supposed to pass some parameters to test.php, have test.php perform some tests based on these parameters, and return the (jason encoded) test result (status_general) to .get(). However, the console.log call in the code below shows a value undefined, while my php debugger shows that test.php returns the proper jason encoded test result. My .get code is based on example 5 of http://api.jquery.com/jQuery.get.
Any suggestions what goes wrong/how to fix it? Thanks in advance for the help.
Code:
$.get( "test.php", { command: "test", val1: ipaddress, val2: "" }) .done(function(data) { var test_result = data.status_general; console.log('test result: '+test_result); },"json");最满意答案
您的PHP页面没有返回有效的json(基于您所说的评论)。 为了让jquery解析json,它不能包含任何其他东西(即html标签)。 发送数据类型的json头也是一个好主意。
http://www.caveofprogramming.com/php/php-json-an-example-javascript-json-client-with-php-server/
data.status_general返回undefined,因为数据不是javascript对象。
Your PHP page isn't returning valid json (based on the comment you said). In order to get jquery to parse the json, it can't have any other stuff in it (ie html tags). Also a good idea to send the data-type json headers.
http://www.caveofprogramming.com/php/php-json-an-example-javascript-json-client-with-php-server/
The data.status_general is returning undefined because data isn't a javascript object.
更多推荐
发布评论