2009-12-27 40 views
1

我有以下代碼,目前即時在我的本地機器上運行。我正在調用一個名爲getxml.php的php腳本,它應該將xml文件的內容作爲響應。

但是,代替GET請求,在Firebug中,我看到正在進行OPTIONS請求,如 OPTIONS getxml.php。我想即時通訊沒有提出跨域Ajax請求,但仍然面臨着這個問題。有任何解決這個問題的方法嗎 ?

var employee_list = new Object; 
$(function(){ 
       $.ajax({ 
       type:"GET", 
       url:"http://localhost/projectname/getxml.php", 
       datatype:"xml", 
       success: function(xml){ 
        $(xml).find('employee').each(function(){ 
         var name_text = $(this).find('name').text(); 
         var url_text = $(this).find('url').text(); 
         employee_list[name_text] = url_text; 
         $('<li></li>').html(name_text + ' (' + url_text + ')').appendTo('#update-target ol'); 

        }); 
       } //closing function   
      }); //closing $.ajax 

}); //closing $(

getxml.php

<?php 
    //Send the xml file as response 
    header('Content-type: text/xml'); 
    readfile('employee_results.xml'); 
?> 

謝謝

+0

其中是PHP中的XML?您只需發送一些標題 – streetparade

+0

感謝您的回覆streetparade。readfile()函數讀取xml文件的內容併發送它。 – samw

+0

實際上沒有這樣的東西作爲HTTP中的OPTIONS請求。即使你設置了'datatype:'OPTIONS'',底層的XHR對象也不會理解這一點。是什麼讓你相信HTTP動詞是「OPTIONS」? – jpsimons

回答

1

確保getxml.php存在。選項通常意味着你有輕微的拼寫錯誤。

+0

感謝您的回覆Ramblingwood。我確信getxml.php存在。我的項目文件夾包含一個js子文件夾,getxml.php,xml文件本身和一個包含上述javascript的index.html頁面。js子文件夾包含jquery庫 – samw

1

更改datatypedataType並查看是否可以解決您的問題。其餘的代碼看起來是正確的。

編輯:另外,我不是一個PHP專業人員,但我寫了一個使用類似方法的地圖應用程序。爲了返回xml,我使用了:

header("Status: 200"); 
header("Content-type: text/xml"); 
echo file_get_contents($q,0); /*$q is the query/filename*/ 
exit(); 

我記得在某處閱讀header("Status: 200");是必需的。

編輯:這是我做了同樣的事情。我希望這有幫助。

/* call ajax method to retrieve earthquakes */ 
    $.ajax({ 
    type: "GET", 
    url: "../getxml.php?q=" + xmlLocation, 
    dataType: "xml", 
    success: function(xml){ 
      $(xml).find('entry').each(function(){ 
       /* Retrieve all needed values from XML */ 
       var title = $(this).find('title').text(); 
       var summary = $(this).find('summary').text(); 
       var coord = $(this).find('georss\\:point').eq(0).text(); 
       if(!coord){var coord = $(this).find('point').text();}; 
       var points = coord.split(' '); 
       var latitude = parseFloat(points[0]); 
       var longitude = parseFloat(points[1]); 
       var htmlString = "<div class=\"infowindow\"><b>" + 
           title + "</b>" + "<p>" + summary + "<br></div>"; 
       var myLatlng = new google.maps.LatLng(latitude,longitude); 
       var marker = new google.maps.Marker(
       { 
       position: myLatlng, 
       map: map, 
       title: title 
       }); 
       markers[markers.length] = marker; 
       addInfoWindow(marker, map, htmlString); 

       $('#output').text("Showing " + markers.length + " earthquakes"); 
      });/* end each */ 
     } 
    }); /* end $.ajax */ 

這個php文件與我上面發佈的完全相同,只是用「安全」來響應ajax請求。

+0

感謝您的回覆吉姆。我做了你所建議的改變,但仍然存在相同的問題:( – samw

+0

這很奇怪,我要添加一個例子來說明如何從USGS.gov爲我製作的地震跟蹤器提取地震XML提要:http:// www .ipreferjim.com /地圖/ quakes.html –