0
Follow
0
View

js http request how to write

shouxianer 注册会员
2023-02-27 11:31
http://ip:端口/sms/smsapi/getTpllist'); //设置请求头 xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); xhr.send('apikey:26743791-298c-4d6e-80de-31af53382697'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status >= 200 && xhr.status < 300) { //服务端返回的结果 console.log(xhr.getAllResponseHeaders()); } } } .
< !- - - - - >
dxydiedre 注册会员
2023-02-27 11:31

I have tried all the above methods, and they are guaranteed. net::ERR_CONNECTION_REFUSED

dxl19861227 注册会员
2023-02-27 11:31

using ajax backend sends the request format: < br / > < br / > $. Ajax({< br / > common attributes: < br / > url: Requested server url
async:(Default: true) By default, all requests are asynchronous. If you want to send a synchronization request, set this option to false.
data: data sent to the server, either as a key-value pair or as a js object
type:(Default: "GET") request method(" POST "or" GET "), the default is "GET" < br / > dataType: The type of data expected to be returned, The value can be xml, html, script, json, text, _defaul, etc.
success:function(result){
Callback function after successful request
result indicates the callback value
},
error:function(){
This function is called when a request fails and an exception occurs on the server.
}
Note: The above properties are case sensitive.
The most powerful aspect of this method is that it can give friendly hints when handling server exceptions.
});

cht9931 注册会员
2023-02-27 11:31

This answer quotes ChatGPT

In JavaScript, you can use the XMLHttpRequest object or the Fetch API to send HTTP requests and fetch remote resources.

Here is sample code for sending a POST request using the XMLHttpRequest object:


const xhr = new XMLHttpRequest();
const url = "http://ip:端口/sms/smsapi/getTpllist";
const apikey = "26743791-298c-4d6e-80de-31af53382697";

xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

xhr.onreadystatechange = function () {
  if (xhr.readyState === 4 && xhr.status === 200) {
    const response = xhr.responseText;
    console.log(response); // 处理获取到的响应数据
  }
};

const data = {
  apikey: apikey,
};

xhr.send(JSON.stringify(data));

Example code for sending a POST request using the Fetch API:

const url = "http://ip:端口/sms/smsapi/getTpllist";
const apikey = "26743791-298c-4d6e-80de-31af53382697";

const options = {
  method: "POST",
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
  body: JSON.stringify({ apikey }),
};

fetch(url, options)
  .then((response) => response.text())
  .then((data) => console.log(data))
  .catch((error) => console.log(error));


Note that cross-domain access to external resources exists in JavaScript. If your website and interface service are not in the same domain name, you need to configure cross-domain access on the server side. Note If you access HTTPS websites, pay attention to the SSL certificate.

potatobroth 注册会员
2023-02-27 11:31

The little girl referred to bing and GPT part of the content write :
Access to the Internet http request, you can use the JavaScript XMLHttpRequest object. The XMLHttpRequest object helps us make an asynchronous HTTP request without blocking other operations on the current page.

In general, the steps to use XMLHttpRequest are as follows:
  1. Creates an XMLHttpRequest object;
  2. Opens a connection;
  3. sends a request to the server;
  4. listens to the readystatechange event and determines whether the request is successful according to the status code;
  5. Gets the data returned by the server.

Here is an example of implementing an extranet http request using XMLHttpRequest:

let xhr = new XMLHttpRequest();  //创建一个XMLHttpRequest对象
xhr.open('GET', 'http://www.example.com/', true);  //打开一个连接,true表示异步请求
xhr.send(null);   //向服务器发送请求
xhr.onreadystatechange = function(){  //监听readystatechange事件
  if(xhr.readyState == 4 && xhr.status == 200){  //判断状态码是否为200200表示请求成功
    let responseText = xhr.responseText;  //获取服务器返回的数据
    console.log(responseText);  //将数据打印在控制台上
  }
}

The answer is not easy, so take it.

About the Author

Question Info

Publish Time
2023-02-27 11:31
Update Time
2023-02-27 11:31