Alex Mcp
Alex Mcp

Reputation: 19315

What should a PHP generate to give back to a jQuery AJAX request?

Perhaps it's a syntax error, but I never assume that. I have a -dead- simple AJAX test set up:

http://www.mcphersonindustries.com/bucket/api.php is a file with simply:

<?php echo "test"; ?>

And I have Apache as localhost with this jQuery bit running:

$(document).ready(function() {

function doAjaxPost() {  
    $.ajax({  
       type: "POST",  
       url: "http://www.mcphersonindustries.com/bucket/api.php",  
       data: "null",  
       success: function(resp){   
           console.log("Response: '" + resp + "'");  
       },  
       error: function(e){  
           console.log('Error: ' + e);  
       }  
    });  
}  

doAjaxPost();

});

So Firebug spits out Response: '' each time, but nothing's coming through the request. Do I need to declare a header in PHP? Am I making a boneheaded mistake somewhere?

Thanks for the insights, as always.

Upvotes: 1

Views: 180

Answers (5)

cletus
cletus

Reputation: 625087

In your example your Javascript is expecting back XML or HTML. You can be explicit about this but specifying the dataType parameter. Is that what you want to return? If so, PHP should do this:

<?php
echo '<p>hello world</p>'; // "text/html" is implied
?>

Also, I'm not sure why you're defining the function inside document ready rather than globally. Perhaps that's an issue.

Commonly people want to return JSON instead, in which case it should look like this:

<p>What is your name?</p>
<input type="text" id="name">
<input type="button" id="send" value="Send">

with

$(function() {
  $("#send").click(doAjaxPost);
});

function doAjaxPost() {
  $.ajax({
    type: "POST",
    url: "http://www.mcphersonindustries.com/bucket/api.php",  
    data: {
      name: $("#name").val()
    },
    dataType: "json",
    success: function(resp) {   
      alert(resp.result);
    },  
    error: function(e){  
      console.log('Error: ' + e);  
    }
  });
}

and PHP:

<?php
header('Content-Type: application/json');
$name = $_POST['name'];
$out = array('result' => 'Hello, ' . $name);
echo json_encode($out);
?>

Upvotes: 2

dd.
dd.

Reputation: 952

You can't do cross-site AJAX without that domain authorizing you:

http://ejohn.org/blog/cross-site-xmlhttprequest/

Upvotes: 2

Paul Creasey
Paul Creasey

Reputation: 28834

At a guess, it could possibly be because jquery is deriving the data type from the server http response header as text/html, yet the actual value is not html.

It seems like a stretch, but it's all i can come up with!

Upvotes: 0

user287107
user287107

Reputation: 9417

do you execute the script from the same host, or is it another location. If thats the case, you need to use the JSONP method. This method allows to create cross domain function calls.

In the JSONP process, the calling javascript creates a unique name for a function, and sends this to the calling script via a get request. the php script creates a javascript file, with a variable of that name, and returns that to the client which includes and executes that piece of script.

Upvotes: 1

Frank Schmitt
Frank Schmitt

Reputation: 25775

If it's a single string, number, or boolean, you can just echo it in your PHP script. If it's a more complex type (like an array of things, or hierarchical data), JSON is the most lightweight way of transferring it.

Generally you can just take a PHP array or object and call echo json_encode() with it as an argument.

To parse it just add dataType: 'json' to your hash.

Upvotes: 1

Related Questions