【问题标题】:Can't pass parameters to $.getJSON()无法将参数传递给 $.getJSON()
【发布时间】:2015-11-11 20:31:52
【问题描述】:

我正在使用 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 网页。

【问题讨论】:

    标签: javascript jquery json zepto appjs


    【解决方案1】:

    你确定你正确加载了 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'=&gt;$user]) 而不是自己制作字符串并获得相同的输出。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 它可能有效,但是我将新文件上传到网络服务器,现在它不会加载到 kik 浏览器中,它说:Console.ERROR: Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png (data:text/html,chromewebdata:12)我不知道你是否熟悉 android 但我不知道到底是什么触发了这个
      【解决方案3】:

      如果你愿意,你可以通过url传递你的参数

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多