繁体   English   中英

无法将参数传递给$ .getJSON()

[英]Can't pass parameters to $.getJSON()

我正在使用jQuery和PHP与我的MySQL数据库进行通信。 jQuery调用PHP脚本并传递参数以进行查找,然后PHP遍历我的MySQL数据库,将其转换为JSON,然后将JSON回显给jQuery。 从理论上讲应该起作用。 它以正确的格式和全部返回jQuery,但是当我在$ .getJSON()中使用可选的data参数时遇到问题。 这是我在做什么:

// I would like to send a string to the php file on my webserver
$.getJSON('http://garbagewire.tk/server/refresh.php', { user:"jacob.pickens" }, function(data) {
    console.log(data.ammount);
});

但是,我从没回过数据,我在这里得到这个错误:

08-18 13:35:01.866  17420-17420/? W/WebConsole﹕ Console.ERROR: Uncaught TypeError: Object #<Object> has no method 'call' (http://garbagewire.tk/zepto.js:2)

这是我的php :(省略了MySQL的内容)

<?php 

$user = $_GET['user'];

echo "{";
echo "\"ammount\":", json_encode($user);
echo "}";
?>

我正在使用App.js api制作kik网页(如果这很重要)。

确定要正确加载jQuery吗? 试用jQuery.ajax,您应该能够通过getJSON传递数据

http://api.jquery.com/jquery.getjson/

$.getJSON( "test.js", { name: "John", time: "2pm" } )
   .done(function( json ) {
   console.log( "JSON Data: " + json.users[ 3 ].name );
})
.fail(function( jqxhr, textStatus, error ) {
   var err = textStatus + ", " + error;
   console.log( "Request Failed: " + err );
});

您也可以执行echo json_encode(['amount'=>$user])而不是自己制作字符串并获得相同的输出。

试试这个,我想你对$ .post语法感到困惑

$.getJSON('http://garbagewire.tk/server/refresh.php?user=jacob.pickens', function(data) {
    console.log(data.ammount);
});

如果需要,可以通过url传递参数

$.getJSON('http://garbagewire.tk/server/refresh.php?user=jacob.pickens', function(data) {
    console.log(data.ammount);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM