I have a form that submits to a PHP script with Jquery and Ajax. The PHP script returns some XML. For some reason the Ajax success function is not firing, and the error ones is.
Can anybody see where I'm going wrong?
My Jquery is as follows
$('#submit-excuse').submit(function (event) {
event.preventDefault();
ws_url = 'http://jacamo.epiphanydev2.co.uk/content/inc/excuse-submit.php?excuse='+$('input#excuse').val();
$.ajax({
type: 'GET',
url: ws_url,
dataType: "xml",
beforeSend: function() {
$('p#response').text('Sending.');
},
success: function(xmlIn) {
results = xmlIn.getElementsByTagName("ReportID");
},
error: function() {
$('p#response').text('Error.');
}
});
});
And my PHP script is as follows:
$excuse = $_GET['excuse'];
$badwords = array (
'one',
'two',
'three',
'four',
'five'
);
if ($excuse == '') {
$error = 'enter something';
} else {
foreach ($badwords as $word) {
$pos = strpos($excuse, $word);
if($pos !== false) {
$passed = false;
}
}
if ($passed !== false) {
$username = 'xxxxx';
$password = 'xxxxx';
$message = $excuse;
$url = 'http://twitter.com/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$passed = 'yes';
}
echo "<?xml version='1.0' encoding='UTF-8'?>
";
echo "\t<result>
";
echo "\t\t<passed>" . $passed . "</passed>
";
echo "\t</result>";
}
Thanks
Jquery is expecting an xml response back from your query. You need to send the xml header. Try adding before your xml header ("content-type: text/xml");
and see if that works
AJAX can not be used across different domains, for this, look into JSON.
This is how I used it :
JQuery-side :
var server = ‘http://www.yoursite.com/path/to/map/’;
var name = ‘Peter’;
var lastname = ‘Marcoen’;
$.getJSON(server+’getData.php?callback=?’,{name:name,lastname:lastname}, function(data) {
alert(data); //Do your thing
});
PHP-side :
$name = $_REQUEST['name'];
$lastname = $_REQUEST['lastname'];
$return = ‘Hello ‘ . $name . ‘ ‘ . $lastname;
echo $_GET['callback'] . ‘(’ . json_encode($return) . ‘)’;