0

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

// 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 网页。

4

3 回答 3

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])代替自己制作字符串并获得相同的输出。

于 2015-08-18T18:51:26.857 回答
1

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

$.getJSON('http://garbagewire.tk/server/refresh.php?user=jacob.pickens', function(data) {
    console.log(data.ammount);
});
于 2015-08-18T18:43:14.590 回答
1

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

$.getJSON('http://garbagewire.tk/server/refresh.php?user=jacob.pickens', function(data) {
    console.log(data.ammount);
});
于 2015-08-18T18:43:53.683 回答