简体   繁体   中英

No response from PHP code using jQuery post()

I am trying to retrieve data from a PHP file the reads a mySQL db. The data comes in fine on a browser:

http://www.primaryaccess.org/REST/geteasyfile.php?id=25

But when I try to access it using jQuery's get() or post() methods, I get no response. The response header in Firebug says there is the right amount of data, but nothing shows up:

$.get("http://www.primaryaccess.org/REST/geteasyfile.php?id=25",function(data) { alert(data); });

Here's the PHP:

<?
$id=$_GET['id'];
$query="SELECT * FROM easyfile WHERE id = '$id'";
$result=mysql_query($query);
mysql_close();
if (($result == false) || (!mysql_numrows($result)))
    echo "Can't load file!";
else    
    echo mysql_result($result,0,"data");
?>

Thanks!

Bill

probably your problem is in the url, you already setted id=25 and then you concatenate the id variable

Hope this help

Try getJSON and replace alert() with console.log() . Additionally, your URL looks strange ( 'id=25' + id ). I hope you also noticed that there is a Same Origin Policy in JS, so you need to upload your testfile to the specified domain.

Try using a json callback

JS

$.ajax({
  url: "http://www.primaryaccess.org/REST/geteasyfile.php?id=25&jsoncallback=?",
  success: function(data) {
    alert(data)
  },
  error: function(error){
    alert(JSON.stringify(error));
  }
});

PHP

<?
$id=$_GET['id'];
$query="SELECT * FROM easyfile WHERE id = '$id'";
$result=mysql_query($query);
mysql_close();
if (($result == false) || (!mysql_numrows($result)))
    $result = "Can't load file!";
else    
    $result = mysql_result($result,0,"data");

$result = array('result' => $result);

echo $_GET['jsoncallback'] . '(' . json_encode($result) . ');'

?>

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