Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数运行后才定义AJAX api调用的localStorage.setItem_Javascript_Jquery - Fatal编程技术网

Javascript 函数运行后才定义AJAX api调用的localStorage.setItem

Javascript 函数运行后才定义AJAX api调用的localStorage.setItem,javascript,jquery,Javascript,Jquery,试图找出为什么我的函数中的localStorage.setItem直到最后才运行 jquery-3.3.1.min.js:2选项404(未找到) 在尝试了第二个api调用之后,console.log id号排在最后。根据我的错误,第二个api调用是有意义的。我知道很难帮助测试和验证,但如果有人能帮助,那就太好了 0: {apiGatewayVersion: "1.24", activeTime: "0", id: 75 } 1: {apiGatewayVersion: "1.24", activ

试图找出为什么我的函数中的localStorage.setItem直到最后才运行

jquery-3.3.1.min.js:2选项404(未找到)

在尝试了第二个api调用之后,console.log id号排在最后。根据我的错误,第二个api调用是有意义的。我知道很难帮助测试和验证,但如果有人能帮助,那就太好了

0: {apiGatewayVersion: "1.24", activeTime: "0", id: 75 }
1: {apiGatewayVersion: "1.24", activeTime: "0", id: 76 }
2: {apiGatewayVersion: "1.24", activeTime: "0", id: 77 }
3: {apiGatewayVersion: "1.24", activeTime: "0", id: 78 }
length: 4


function startSession() {
    var _url = "http://169.254.10.10:8080/api/v0/sessions"
    $.ajax({
        "url": _url,
        "method": "GET",
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
    })
        .then(function (response) {
            var max = 0;
                for (var property in response) {
                max = (max < parseFloat(property)) ? parseFloat(property) : max;
                }
            var data = JSON.stringify(response[max]);
            var parse = JSON.parse(data);
            var id = parse.id;
            localStorage.setItem('id', id);
        console.log(localStorage.id);
});
    var _url2 = "http://169.254.10.10:8080/api/v0/sessions" + localStorage.id + "/operations/start"
    $.ajax({
        "url": _url2,
        "method": "POST",
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
});
    alert("Session Starting, Please wait...")
}
0:{apiGatewayVersion:“1.24”,activeTime:“0”,id:75}
1:{apiGatewayVersion:“1.24”,activeTime:“0”,id:76}
2:{apiGatewayVersion:“1.24”,activeTime:“0”,id:77}
3:{apiGatewayVersion:“1.24”,activeTime:“0”,id:78}
长度:4
函数startSession(){
var_url=”http://169.254.10.10:8080/api/v0/sessions"
$.ajax({
“url”:,
“方法”:“获取”,
“超时”:0,
“标题”:{
“内容类型”:“应用程序/json”
},
})
.然后(功能(响应){
var max=0;
for(响应中的var属性){
max=(max
您不能以
localStorage.id

并修复您的代码:

var _url2 = "http://169.254.10.10:8080/api/v0/sessions/" + localStorage.getItem('id') + "/operations/start"

由于您在可能发生竞争条件的情况下使用的是承诺行为,请确保在成功获得会话后开始操作

我看不到在操作启动期间使用本地存储的逻辑原因:

function startSession() {
    var _url = "http://169.254.10.10:8080/api/v0/sessions"
    var request = $.ajax({
        "url": _url,
        "method": "GET",
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
    });
    request.then(function (response) {
      /*
      [
        {apiGatewayVersion: "1.24", activeTime: "0", id: 75 },
        {apiGatewayVersion: "1.24", activeTime: "0", id: 76 },
        {apiGatewayVersion: "1.24", activeTime: "0", id: 77 },
        {apiGatewayVersion: "1.24", activeTime: "0", id: 78 }
      ]
      */
      // Your session list is sorted by id order, 
      // and last session is last object in array
      // it's enough to get last object from response array
      var lastSession = (Array.isArray(response) && response.length)
                        ? response[response.length-1]
                        : {id: 0};
      localStorage.setItem('id', lastSession.id);

      var _url2 = "http://169.254.10.10:8080/api/v0/sessions/" + localStorage.getItem('id') + "/operations/start";
      // or simply:
      // var _url2 = "http://169.254.10.10:8080/api/v0/sessions/" + lastSession.id + "/operations/start";
      $.ajax({
        "url": _url2,
        "method": "POST",
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
      }).then(function(response) {
        console.log(response);
      });

      alert("Session Starting, Please wait...")
    });
}

注意:如果响应是对象而不是数组:


      /*
      {
       0: {apiGatewayVersion: "1.24", activeTime: "0", id: 75 },
       1: {apiGatewayVersion: "1.24", activeTime: "0", id: 76 },
       2: {apiGatewayVersion: "1.24", activeTime: "0", id: 77 },
       3: {apiGatewayVersion: "1.24", activeTime: "0", id: 78 }
      }
      */
      response = Object.values(response); // add this
      var lastSession = (Array.isArray(response) && response.length)
                        ? response[response.length-1]
                        : {id: 0};

您不能以
localStorage.id

并修复您的代码:

var _url2 = "http://169.254.10.10:8080/api/v0/sessions/" + localStorage.getItem('id') + "/operations/start"

由于您在可能发生竞争条件的情况下使用的是承诺行为,请确保在成功获得会话后开始操作

我看不到在操作启动期间使用本地存储的逻辑原因:

function startSession() {
    var _url = "http://169.254.10.10:8080/api/v0/sessions"
    var request = $.ajax({
        "url": _url,
        "method": "GET",
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
    });
    request.then(function (response) {
      /*
      [
        {apiGatewayVersion: "1.24", activeTime: "0", id: 75 },
        {apiGatewayVersion: "1.24", activeTime: "0", id: 76 },
        {apiGatewayVersion: "1.24", activeTime: "0", id: 77 },
        {apiGatewayVersion: "1.24", activeTime: "0", id: 78 }
      ]
      */
      // Your session list is sorted by id order, 
      // and last session is last object in array
      // it's enough to get last object from response array
      var lastSession = (Array.isArray(response) && response.length)
                        ? response[response.length-1]
                        : {id: 0};
      localStorage.setItem('id', lastSession.id);

      var _url2 = "http://169.254.10.10:8080/api/v0/sessions/" + localStorage.getItem('id') + "/operations/start";
      // or simply:
      // var _url2 = "http://169.254.10.10:8080/api/v0/sessions/" + lastSession.id + "/operations/start";
      $.ajax({
        "url": _url2,
        "method": "POST",
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
      }).then(function(response) {
        console.log(response);
      });

      alert("Session Starting, Please wait...")
    });
}

注意:如果响应是对象而不是数组:


      /*
      {
       0: {apiGatewayVersion: "1.24", activeTime: "0", id: 75 },
       1: {apiGatewayVersion: "1.24", activeTime: "0", id: 76 },
       2: {apiGatewayVersion: "1.24", activeTime: "0", id: 77 },
       3: {apiGatewayVersion: "1.24", activeTime: "0", id: 78 }
      }
      */
      response = Object.values(response); // add this
      var lastSession = (Array.isArray(response) && response.length)
                        ? response[response.length-1]
                        : {id: 0};

感谢您的帮助,我使用localStorage是为了让会话id一直保持到用户注销,在用户注销时,删除操作将发送到api网关。看起来我也可以使用sessionstorage。我们可能不会有太多的同时用户,所以我可能不需要使用这两种方法option@PA_Commons在任何情况下,我给出的答案都是不带localstorage的alternative(参见代码中的注释)。但是是的,会话管理不应该在客户端进行。客户端唯一应该保留的是用于内部授权的会话id(访问令牌)。否则,您将允许用户手动调用不同用户的会话操作开始(可能不是)。感谢您的帮助,我使用了localStorage,以便在用户注销之前保留会话id,在用户注销后,删除操作将发送到api网关。看起来我也可以使用sessionstorage。我们可能不会有太多的同时用户,所以我可能不需要使用这两种方法option@PA_Commons在任何情况下,我给出的答案都是不带localstorage的alternative(参见代码中的注释)。但是是的,会话管理不应该在客户端进行。客户端唯一应该保留的是用于内部授权的会话id(访问令牌)。否则,您将允许用户手动调用不同用户的会话操作开始(可能不是)。