views:

45

answers:

3

What is the problem with Ajax (jQuery) And Php ? Why my code does not work ?

jQuery Code:

$(document).ready(function(){   

    $.ajax({
    type: "GET",
    url: "Tags.php",
    dataType: "xml",
    success: function(xml) {
    alert("success");
    }
    }); 

});

Tags.php Code

<?xml version="1.0" encoding="UTF-8"?>
<tages>
<?php echo "<tag>hello</tag>"; ?>
</tages>
A: 

Tags.php is not a URL. You probably need a full URL: http://www.foo.com/Tags.php.

You'll find using all lowercase filenames a good idea.

gregjor
Yes it is a relative URL.
Petah
It doesn't work :)
faressoft
A: 

Sorry, but "My code does not work" isn't specific enough. In what way does it not work? Have you tried viewing the output of Tags.php directly in the browser to see that it contains what you're expecting it to contain?

One thing to bear in mind, though, is PHP short tags causes issues with the XML preamble, because both use <? to mark where they start. Either turn short tags off, or echo() the XML preamble. The first solution is the preferred one.

Other than that, without more information, I can't help.

Gordon
It is not preferable to turn off short tags, as many 3rd party scripts use short tags and you will break compatibility.
Petah
But as the OP has discovered, short tags cause issues with anything that starts with an XML preamble. Besides, if you write code that uses short tags and move it to a server where they're disabled, you'll break compatibility. Short tags can be off or on for a given server, whereas normal tags are always guaranteed to be available. Finally, as I understand the situation, short tags are expected to become depreciated as of PHP 6.
Gordon
+1  A: 

you need to

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

instead of

<?xml version="1.0" encoding="UTF-8"?>

because the <? will get interpreted by PHP and cause a syntax error.

Petah