表单提交是HTML网页发送HTTP请求的唯一方式吗?

时间:2017-12-31 14:07:53

标签: html http

HTML网页提供多少种方式来发送HTTP请求?

https://rawgit.com/markroche92/SDND-Traffic-Sign-Classification/master/structure.JPG structure.JPG 元素中的表单提交是否是HTML网页发送HTTP请求的唯一方式?

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式。

  1. 使用常规表单提交POST和GET

    <a href="something.html">Click here</a>
  2. 使用超链接获取

    $.ajax({ url : 'https://reqres.in/api/users?page=2', type : 'GET/POST/PUT/DELETE', data : data, //For POST/PUT/Delete requests success : function(data) { //do response processing here } });

  3. 使用AJAX进行POST,PUT,DELETE和GET

        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/server', true);
    
        xhr.onload = function () {
          // Request finished. Do processing here.
        };
    
        xhr.send(null);
        // xhr.send('string');
        // xhr.send(new Blob());
        // xhr.send(new Int8Array());
        // xhr.send({ form: 'data' });
        // xhr.send(document);
    
  4. 使用XMLHttpRequest POST,PUT,DELETE和GET

  5. GET ::

        var xhr = new XMLHttpRequest();
        xhr.open("POST/PUT/DELETE", '/server', true);
    
        //Send the proper header information along with the request
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
        xhr.onreadystatechange = function() {//Call a function when the state changes.
        if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
            // Request finished. Do processing here.
        }
        }
        xhr.send("foo=bar&lorem=ipsum"); 
        // xhr.send('string'); 
        // xhr.send(new Blob()); 
        // xhr.send(new Int8Array()); 
        // xhr.send({ form: 'data' }); 
        // xhr.send(document);
    

    POST / PUT / DELETE ::

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ViewControllerTableViewCell
        cell.locationLabel.text = eventsList[indexPath.row].location
        cell.eventTimeLabel.text = eventsList[indexPath.row].dateTime
        cell.eventTypeLabel.text = eventsList[indexPath.row].agencyEventSubtypeCode
        cell.agencyIdLabel.text = eventsList[indexPath.row].agencyId
    
        if alarmLevel != "0" {
          cell.locationLabel.textColor = UIColor.red
        } else {
          cell.locationLabel.textColor = UIColor.black
        }
        return cell
    }