简体   繁体   中英

Can't pass parameters to $.getJSON()

I'm using jQuery and PHP to communicate with my MySQL database. jQuery calls a PHP script and passes along the parameters to look up, then PHP looks through my MySQL database, turns it into JSON and then echos the JSON back to my jQuery. In theory that should work. It returns the jQuery in the correct format and all, however I run into a problem when I use the optional data parameter in $.getJSON(). Heres what I'm doing:

// 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);
});

However, I never get the data logged back, I get this error here:

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)

And here's my php: (MySQL stuff omitted)

<?php 

$user = $_GET['user'];

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

I'm using the App.js api to make a kik webpage if that is to any importance.

Are you sure you're loading jQuery correctly? Try jQuery.ajax you should be able to pass data through 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 );
});

Also you can do echo json_encode(['amount'=>$user]) instead of making the string yourself and get the same output.

Try this, I guess you got confused with $.post syntax

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

If you want, you can pass your parameters through the url

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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