tags:

views:

54

answers:

3

Hi, 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'?>\n";
    echo "\t<result>\n";
    echo "\t\t<passed>" . $passed . "</passed>\n";
    echo "\t</result>";
}

Thanks

A: 

without looking too deep into your post, you are using an absolute path in your ajax request. Is that the same domain your script is laying on?

If not, the ajax same origin policy will deny the request.

Kind Regards

--Andy

jAndy
Hi jAndy, it is on the same domain so the same origin policy shouldn't affect it.
Probocop
+1  A: 

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

pitch_invasion
Perfect, that sorted it. Thanks!
Probocop
A: 

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) . ‘)’;
Pmarcoen