开发者

JQuery AJAX GET returning an empty file

开发者 https://www.devze.com 2022-12-16 05:12 出处:网络
I am trying to grab http://lib.softvoyage.com/cgi-bin/gate_dest_hotels.xml?code_ag=nwi&alias=tpi&language=en&with_cdata=y via AJAX however it keeps returning nadda (firebug screenshot here

I am trying to grab http://lib.softvoyage.com/cgi-bin/gate_dest_hotels.xml?code_ag=nwi&alias=tpi&language=en&with_cdata=y via AJAX however it keeps returning nadda (firebug screenshot here: http://img683.imageshack.us/img683/3279/firebug.jpg)

Below is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm开发者_Python百科l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Search Form</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    </head>
    <body>
        <form method="post" action="#">
            <label for="gateway_dep">Leaving From:</label>
            <select id="gateway_dep" name="gateway_dep">
                <option value="">Select a gateway</option>
                <option value=""></option>
                <option value="">Loading gateways</option>
            </select>
        </form>
        <script type="text/javascript">
        <!--
            $(document).ready(function() {
                $.ajax({
                    url:"http://lib.softvoyage.com/cgi-bin/gate_dest_hotels.xml?code_ag=nwi&alias=tpi&language=en&with_cdata=y",
                    cache:false,
                    dataType:"text",
                    type:"GET",
                    success:function(xml) {
                        alert(xml);
                    }
                });
            });
        -->
        </script>
    </body>
</html>


You can only use AJAX on pages within the same domain as the current page. If you want to get data from an external source you will have to use the getJSON function and the other site will have to return the data in a form that getJSON can process.

Alternatively you could have a server side proxy that does the get request on the server side where the AJAX limitations do not exist and then return the data to your page.


You can't make requests via the $.ajax call to another domain, a good post for work rounds is here: http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/ This is a security measure to prevent say, your ad/iframe on a page collecting my login information on the page then uploading it to another site.

Your best bet is a proxy on your own domain, or alternatively use the YQL approach and go through yahoo, this is detailed in the blog above.

0

精彩评论

暂无评论...
验证码 换一张
取 消