简体   繁体   English

如何强制JQuery发出HTTP 1.0请求并关闭连接

[英]How to force JQuery to make an HTTP 1.0 request and close the connection

I'm new to jQuery and would like to make a POST request using plain HTTP 1.0 with connection close, instead of the HTTP 1.1 connections that is currently kept alive. 我是jQuery的新手,并希望使用简单的HTTP 1.0(关闭连接)而不是当前保持活动的HTTP 1.1连接发出POST请求。

Below is an is the onClick handler for the button that does the post. 以下是执行发布的按钮的onClick处理程序。

$( "#certGenBtn" ).click( function( event ) {
$.ajax({
  type: "POST",
  url: "http://localhost:9099/",
  data: { testVal : "data" },
  success: function( data ) {
              alert( "whoops" );
           },
  beforeSend: function( xhr ) {
                  xhr.setRequestHeader( "Connection", "close" );
              }
    });
});

The raw output dumped on the server side is the following: 在服务器端转储的原始输出如下:

POST / HTTP/1.1
Host: localhost:9099
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: */*
Accept-Language: en-ZA,en-GB;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/  
Content-Length: 12
Origin: http://localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

testVal=data

The Connection close header is being 'ignored' and the connection remains open to the server so the very last line testVal=data only gets through once i reload the page (I assume this is because the connection is then closed by the browser). Connection关闭标头被“忽略”,并且连接保持对服务器的打开状态,因此最后一行testVal=data仅在我重新加载页面后才通过(我认为这是因为该连接随后被浏览器关闭了)。 I assume it's something very simple and/or stupid that I'm missing but cannot see anything in the documentation. 我认为这是一个非常简单和/或愚蠢的东西,我丢失了,但是看不到文档中的任何内容。

1) According to the HTTP 1.1 spec, Connection: close is a valid header: 1)根据HTTP 1.1规范, Connection: close是有效的标头:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

How about trying to add it with the headers key instead of beforeSend? 如何尝试使用标头键而不是beforeSend添加它?

.ajax({

   ....
   headers: {Connection: close},

});

2) The jquery docs specify the following function signature for the beforeSend function: 2)jQuery文档为beforeSend函数指定以下函数签名:

beforeSend 
Type: Function( jqXHR jqXHR, PlainObject settings )

So you might also try adding the header to the settings object rather than the jqXHR object. 因此,您也可以尝试将标头添加到设置对象而不是jqXHR对象。

Well, I tested your original code as well as my suggestions, and none of them succeed. 好吧,我测试了您的原始代码以及我的建议,但没有一个成功。 It looks like the XMLHttpRequest spec forbids setting the Connection header: 看起来XMLHttpRequest规范禁止设置Connection标头:

The setRequestHeader(header, value) method must run these steps: setRequestHeader(header,value)方法必须运行以下步骤:

Terminate these steps if header is a case-insensitive match for one of the following headers: 如果标头与以下标头之一不区分大小写,请终止这些步骤:

... Connection ... ...连接...

The above headers are controlled by the user agent to let it control those aspects of transport. 上述标头由用户代理控制,以使其控制传输的那些方面。 This guarantees data integrity to some extent. 这在一定程度上保证了数据完整性。
http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method

var xhr = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});

//kill the request
xhr.abort()

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

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