AJAX GET请求不发送任何内容

时间:2018-03-06 11:24:25

标签: javascript jquery html ajax

我目前正在尝试在AJAX中测试自定义标头,但是当我使用下面的html函数打开JS文件时,没有请求。 Edge仅在大约30秒后才说Response got loaded from cache。 有人知道为什么会这样吗?

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script>
            function load() {
                //$.ajaxSetup({headers: { "CustomHeader": "myValue" }});
                alert("loaded");
            }

            function request() {
                var settings = {
                    "crossDomain": true,
                    "url": "http://localhost:11280/AmbrosiaREST.svc/TestCookie",
                    "method": "GET",
                    "headers": {
                        "Content-Type": "application/json",
                        "SessionID": "IDNOSET",
                        "UserID": "2",
                        "Cache-Control": "no-cache"
                    },
                    "processData": true
                }

                $.ajax(settings).done(function (response) {
                    alert(response);
                }).fail((response) => {
                    console.log(response);
                }).always(() => {
                    console.log("sth");
                })
            }
        </script>
    </head>
    <body onload="load()">
        <button type="button" onclick="request()">Send smth</button>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

根据HTTP版本(1.0,1.1等)和浏览器,这应该适用于某些旧版本,例如IE6。

&#13;
&#13;
function request() {
  var settings = {
    "crossDomain": true,
    "url": "http://localhost:11280/AmbrosiaREST.svc/TestCookie",
    "method": "GET",
    "headers": {
      "Content-Type": "application/json",
      "SessionID": "IDNOSET",
      "UserID": "2",
      "Cache-Control": "no-cache, no-store, must-revalidate",
      "Pragma": "no-cache",
      "Expires": 0
    },
    "processData": true
  }

  $.ajax(settings).done(function(response) {
    alert(response);
  }).fail((response) => {
  console.log("fail");
    console.log(response);
  }).always(() => {
    console.log("sth");
  })
}
request()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;