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

时间:2015-08-18 18:41:21

标签: javascript jquery json zepto appjs

我正在使用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网页,如果这是重要的。

3 个答案:

答案 0 :(得分:2)

您确定要正确加载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])而不是自己创建字符串并获得相同的输出。

答案 1 :(得分:1)

试试这个,我猜你和$ .post语法混淆了

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

答案 2 :(得分:1)

如果需要,您可以通过网址传递参数

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