【发布时间】:2026-02-11 13:15:02
【问题描述】:
我有以下代码,目前我正在本地机器上运行。我正在调用一个名为 getxml.php 的 php 脚本,它应该发送一个 xml 文件的内容作为响应。
但是,在 Firebug 中,我看到正在发出 OPTIONS 请求,而不是 GET 请求,例如 选项 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');
?>
谢谢
【问题讨论】:
-
php中的xml在哪里?你只需发送一些标题
-
感谢streetparade的回复。readfile()函数读取xml文件的内容并发送出去。
-
在 HTTP 中确实没有 OPTIONS 请求这样的东西。即使您设置
datatype:"OPTIONS",底层XHR 对象也不会理解这一点。是什么让你相信 HTTP 动词是“OPTIONS”? -
直接在浏览器中查看getxml.php url会发生什么?