2017-06-29 59 views
0

我得到一个405错误使得从本地主机的请求,这是完全错误:405方法

OPTIONS http://www.myurl.com 405 (Method Not Allowed)

XMLHttpRequest cannot load http://www.myurl.com . Response for preflight has invalid HTTP status code 405

我理解的问题,但怪癖是,我得到这个错误只是当我使用的角度$ http服务:

var req = { 
    method: 'POST', 
    url: 'http://www.myurl.com', 
    headers: { 
     'Content-Type': 'application/json' 
    }, 
    data: { 
    } 
} 

$http(req) 
    .then(function(res) {}, 
     function(error) {}); 

使用XMLHttpRequest的完美的作品:

var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     console.log(xhttp.responseText); 
    } 
}; 
xhttp.open("POST", 'http://www.myurl.com', true); 
xhttp.send(); 

我有一个chrome扩展来添加CORS头文件并且它正在工作。我还注意到,如果我删除了xhttp.open中的第三个参数,那么错误再次出现。

¿有谁知道原因? ¿如何在不出错的情况下使用角度服务?

回答

0

你可以这样写。因为你没有给url发送任何参数。所以我认为这是做这件事的好方法。只是尝试它可能适合你。

$http.post('http://www.myurl.com'). 
success(function(data) { 
    //success Response here 
}); 
0

您需要在服务器端允许OPTIONS方法。在每个GET,POST,PUT,DELETE ...请求之前,启动一个OPTIONS请求。

我建议你禁用你的“chrome扩展来添加CORS”,以使你的最终用户具有相同的配置。