jquery .get没有返回正确的值(jquery .get does not return proper value)

编程入门 行业动态 更新时间:2024-10-27 08:24:04
jquery .get没有返回正确的值(jquery .get does not return proper value)

在下面的函数中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.

更多推荐

本文发布于:2023-07-24 13:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1246293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   jquery   proper   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!