简体   繁体   English

把Http请求放在js中

[英]Put Http Request in js

I want to send a http put request to a api using jquery/js here is what i came up with 我想使用jquery / js向api发送一个http put请求,这就是我想出的

$.ajax({
type: 'PUT',
dataType: 'json',
url: "http://192.168.x.xxx/api/somerandomusername/lights/1/state",
headers: {"X-HTTP-Method-Override, Access-Control-Allow-Origin: *": "PUT"},
data: {"on":true, "sat":255, "bri":255,"hue":10000}
});

if i'm using the api debugging tool it's working so my url and data are good and i don't have anything showing up in the chrome console. 如果我正在使用api调试工具它正在工作,所以我的网址和数据都很好,我没有在chrome控制台中显示任何内容。

For more context here is my setup 有关更多上下文,请参阅我的设置

i'm sending the request to a Philips/hue bridgeusing the url and telling the bulb number 1 this string of data "on":true, "sat":255, "bri":255,"hue":10000. 我正在使用网址向飞利浦/色调桥发送请求,并告诉灯泡编号1这个数据串“on”:true,“sat”:255,“bri”:255,“hue”:10000。

You can try 你可以试试

$.ajax({
    headers: { 'custom-header': 'value' }
});

If you want to add a header (or set of headers) to every request then use the beforeSend hook with $.ajaxSetup(): 如果要为每个请求添加标头(或标头集),请使用带有$ .ajaxSetup()的beforeSend挂钩:

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('custom-header', 'value');
    }
});

and you can read on the header properties 并且您可以阅读标题属性

http://www.w3.org/TR/cors/ http://www.w3.org/TR/cors/

and a possible duplicate 和可能的重复

How can I add a custom HTTP header to ajax request with js or jQuery? 如何使用js或jQuery向ajax请求添加自定义HTTP标头?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM