jQuery - 将HTTP PUT发送到localhost webserver(端口80)

时间:2017-11-12 16:16:36

标签: javascript jquery html http

如果没有谷歌浏览器将请求类型更改为OPTIONS,如何向本地网络服务器(端口80上的localhost)发送HTTP PUT请求?

var json = {
    on: false
};
$.ajax({
    type: "PUT"
    , url: "http://localhost/api/newdeveloper/lights/6/state"
    , contentType: 'text/plain'
    , data: JSON.stringify(json)
}).done(function (data, textStatus, jqXHR) {
    console.log(data);
});

enter image description here

网络服务器发回:

  

Access-control-allow-origin:*

所以我猜CORS不是问题,是吗?

使用XMLHttpRequests会导致同样的问题:

var request = new XMLHttpRequest();
request.open("PUT", "http://localhost/api/newdeveloper/lights/2/state", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () { //Call a function when the state changes.
    if (request.readyState == 4 && request.status == 200) {
        alert(request.responseText);
    }
}
request.send("{\"on\": false}");

0 个答案:

没有答案