亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 Jquery 中保存獲取的數據

無法在 Jquery 中保存獲取的數據

慕容708150 2022-06-09 16:44:19
我正在處理我的前端,我遇到了一個障礙。我正在嘗試從后端獲取數據,它實際上是在獲取數據。但只有在其他一切之后?我會給你看。$(function(){    function GetURLId() {        var url = window.location.href;        var path = url.substring(url.lastIndexOf('/') + 1);        var id = path.substring(path.lastIndexOf('?id=') + 4, path.lastIndexOf("?id") + 5)        return id;    }    var url = 'https://localhost:5001/api/rental/byId/' + GetURLId();        fetch(url)        .then((resp) => resp.json())         .then(function(data) {            Object.keys(data).forEach(key => {                console.log(`${key}: ${data[key]}`);            })        });});所以首先我從 URL 中得到我正在使用的 id。那么問題出在它下面的代碼。我可以在控制臺中獲取我的數據。記錄這個:id: 2status: "Open"damage: true所以數據實際上是從我的后端獲取的。但是現在,每次我嘗試保存數據時,它都未定義。我試過了:$(function(){    var rental = []; // Added an array    var url = 'https://localhost:5001/api/rental/byId/' + GetURLId();        fetch(url)        .then((resp) => resp.json())         .then(function(data) {            Object.keys(data).forEach(key => {                console.log(`${key}: ${data[key]}`);                rental.push(rental[key] = data[key]);            })        });    console.log(rental['id']); // Returns undefined});和:var rental = []; // Added an array outside of the function$(function(){    var url = 'https://localhost:5001/api/rental/byId/' + GetURLId();        fetch(url)        .then((resp) => resp.json())         .then(function(data) {            Object.keys(data).forEach(key => {                console.log(`${key}: ${data[key]}`);                rental.push(rental[key] = data[key]);            })        });    console.log(rental['id']); // Returns undefined});但!最后一個是在函數之外租用的,我實際上可以在控制臺中調用它。在控制臺中,它實際上確實返回了值。Inside Console:> rental["id"]< 2但這也行不通。它在控制臺中返回傷害:未定義 3 次。因此,如果有人知道這里發生了什么,那就太棒了!
查看完整描述

2 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

獲取請求是異步的。這意味著當您調用fetch它時可能需要一段時間才能完成它,因此 JavaScript 允許其余代碼繼續執行而不會阻塞。因此,在您的請求完成之前將任何內容記錄到控制臺顯然會導致一個空數組。


此外,數組是基于索引的,而不是基于 JavaScript 的名稱。但是,由于數組本質上是對象,所以它仍然可以工作,但您永遠不應該執行以下操作。


var rental = [];

rental['id'] = 'foo';

console.log(rental['id']);

而是使用一個普通的對象,該對象旨在以這種方式使用。


var rental = {};

rental['id'] = 'foo';

console.log(rental['id']);

在你的最后一個例子中,你似乎做的一切都很好。您確定您的 fetched在其結構data中沒有值嗎?undefined這將有助于查看數據的樣子。


查看完整回答
反對 回復 2022-06-09
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

答案:我的代碼中有 2 個錯誤。- 首先,我沒有正確考慮代碼的異步性質。- 其次,當試圖用另一個修復它時,然后阻止并在那里執行我的代碼。我沒有在進程 then 塊中返回值,而是返回了 forEach。


fetch(url)

        .then(resp => resp.json()) 

        .then(data => {

            var rentalStatus;

            Object.keys(data).forEach(key => {

                rental[key] = data[key];

                if(key == "status") {

                    rentalStatus = data[key];

                }

            })

            return rentalStatus;

        })

        .then(rentalStatus => {

            console.log(rental["id"]); // This does return the id now!

            if(rentalStatus == "Reserved") {

                $("#assign-items").removeClass("d-none");

            }

        }).catch(error => {

            console.log("Error:" + error);

        });


查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號