当我尝试将 XML 文件传输到服务器时,出现此错误。
在我的场景中,网页使用 javascript 处理 XML 文件,然后将处理过的 XML 文件上传到 PHP 服务器。
文件容量为 400K。
配置文件
post_max_size = 8M
upload_max_filesize = 2M
memory_limit = 128M
Run Code Online (Sandbox Code Playgroud)
客户初始请求:
$.ajax({
type: "GET",
url: "/dirname/filename.xml",
dataType: "xml",
async: false,
success: function(data){
xmlCID = data;
},
error: function(jqXHR, textStatus, errorThrown){
console.log(jqXHR.responseText, textStatus, errorThrown);
alert(textStatus);
}
});
Run Code Online (Sandbox Code Playgroud)
客户端上传请求
$.ajax({
url: "/dirname/filename.xml",
dataType: "xml",
method: "PUT",
processData: false,
data: xmlCID,
success: function(data){
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown){
alert(jqXHR.responseText, textStatus, errorThrown);
}
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,服务器响应是:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> …Run Code Online (Sandbox Code Playgroud) 我的场景由两个webserver组成,一个是本地的,一个是远程的.
本地Web服务器(Apache)处理一个Web应用程序,我想在其中向远程Web服务器(Lighttpd)发出ajax请求.
Ajax请求使用angularjs $ http.
var req = {
method: 'POST',
url: 'http://url/myphp.php',
headers: {
'Authorization': 'Basic ' + btoa('username:password'),
'Content-Type': 'application/x-www-form-urlencoded'
},
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: xmlString
}
$http(req).then(function () {
console.log("OK!");
});
Run Code Online (Sandbox Code Playgroud)
远程php脚本是:
<?php
echo "You have CORS!";
?>
Run Code Online (Sandbox Code Playgroud)
不幸的是我有一个
401 Unhauthorized
XMLHttpRequest cannot load http://url/myphp.php. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had …Run Code Online (Sandbox Code Playgroud)