0

I want to parse an xml document with jquery but it doesn't return anything : Here's my code :

<script type="text/javascript">
     $(document).ready(function(){
        $(#envoi_search).click(function{
            $.get("http://localhost/music.xml", { titre: "Pourtant" }, function (data){
                    alert("Yoo !!") ;}, "xml" );
        });
    });
</script>

the errors :

Uncaught ReferenceError: ajaxget is not defined

XMLHttpRequest cannot load file:///home/mehdi/Bureau/music.xml?titre=Pourtant. Origin null is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest cannot load http://localhost/music.xml?titre=Pourtant. Origin null is not allowed by Access-Control-Allow-Origin.

Do you have any idea about that ? Thank you :-)

3
  • Check the console for errors. May 2, 2012 at 15:42
  • 1
    Should '#evnoi_search' not be presented in quotes? May 2, 2012 at 15:43
  • Thank you :-) @RoryMcCrossan : i just edited the post to show you the console's error puppybeard : i added them but still don't work :\
    – user1079425
    May 2, 2012 at 15:50

1 Answer 1

0

Assuming your site is also running under localhost, try using a relative path to get your XML. Also, the selector needs to be in quotes as others have mentioned. Try this:

$("#envoi_search").click(function {
    $.get(
        "/music.xml", 
        { titre: "Pourtant" }, 
        function (data) {
            alert("Yoo !!");
        }, 
        "xml" 
    );
});

If you are not running under local host, then you will not be able to get the XML from this URL as it will be blocked by the browsers Same Origin Policy. There are plenty of article on that if you have a Google for it.

4
  • thank you it works now, but I still have this error : Uncaught ReferenceError: ajaxget is not defined (anonymous function)ttt.html:40 onclick
    – user1079425
    May 2, 2012 at 15:55
  • No problem. If this answer helped you could you accept it by clicking the greentick to the left. Thanks. May 2, 2012 at 16:03
  • No problem ! One last question please : if I run this script under phonegap which doesn't work on localhost it will work ?
    – user1079425
    May 2, 2012 at 16:04
  • It should as the request will be relative to the site root which shouldn't change. May 2, 2012 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.