第一种是:jquery+ajax,每次上传的时候,显示这个错误,是不是我的url写得有问题,应该怎么写
Failed to load resource: the server responded with a status of 405 (Not Allowed)
index.html:1 XMLHttpRequest cannot load http://localhost/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405.
下面是我的代码
$(".loading").show(); //显示加载图片
//发送数据
$.ajax({
url:'http://localhost',
type:'POST',
data:data,
cache: false,
contentType: false, //不可缺参数
processData: false, //不可缺参数
success:function(data){
data = $(data).html();
//第一个feedback数据直接append,其他的用before第1个( .eq(0).before() )放至最前面。
//data.replace(/</g,'<').replace(/>/g,'>') 转换html标签,否则图片无法显示。
if($("#feedback").children('img').length == 0) $("#feedback").append(data.replace(/</g,'<').replace(/>/g,'>'));
else $("#feedback").children('img').eq(0).before(data.replace(/</g,'<').replace(/>/g,'>'));
$(".loading").hide(); //加载成功移除加载图片
},
error:function(){
alert('上传出错');
$(".loading").hide(); //加载失败移除加载图片
第二个问题是类似的:也是没有上传成功,我只需要有一个能上传成功就行了,我觉得可能是我的url有问题,但这就是我的ip地址
错误显示:
Failed to load resource: the server responded with a status of 403 (Forbidden)
index.html:1 XMLHttpRequest cannot load http://192.168.1.137/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
我的代码如下:
var xhr = new XMLHttpRequest();
.....(中间代码省略就不贴上来了)
xhr.open('post', 'http://192.168.1.137',true);
xhr.onreadystatechange = function () {
alert(xhr.readyState);//FF下会依次是1,2,3,4但最后还会再来个1
//IE下则是1,1,3,4
};
xhr.send(fd);
假设我的ip地址为192.168.1.xxx,然后我想把文件传输到我的/home/hhh/aaa目录下,我应该怎么写url
是因为同源请求了,你要在你的后台代码中的响应头中添加“Access-Control-Allow-Origin”这个字段,就可以访问了