[英]Ajax code working in some parts and not working in others
我的 Ajax 請求從數據庫下載一些數據時遇到問題。
下面有兩個代碼:一個有效,一個無效,盡管它們基本相同。 我稍后還設置了我的代碼以顯示變量( console.log(location)
),但它只是讀取undefined
。
我知道它的 php 部分正在工作,因為我還在 ajax 調用成功時執行了另一個console.log(data)
並返回了我在數據庫中輸入的數據。 發生了什么事,我該如何解決?
不起作用的代碼:
var location;
function downloadCoords() {
$.ajax({
type: 'GET',
url: 'transformerthing.php',
dataType: "json",
success: function(data) {
console.log(data);
location = data.location;
},
error: function(data) {
console.log(data);
}
});
}
有效的代碼:
var mapCode;
var used;
var active;
function downloadCode() {
$.ajax({
type: 'GET',
url: 'getMapCode.php',
dataType: "json",
success: function(data) {
console.log(data);
mapCode = data.mapCode;
used = data.used;
active = data.active;
},
error: function(data) {
console.log(data);
}
});
}
//shorthand deferred way
$.getJSON( "transformerthing.php")
.done(function(data){
console.log(data);
}).fail(function(msg){
console.log(msg)
});
var location;
function downloadCoords() {
$.ajax({
type: 'GET',
url: 'transformerthing.php',
dataType: "json",
success: function(data) {
console.log(data);
location = data.location;
console.log(location);
},
error: function(data) {
console.log(data);
}
});
}
再試一遍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.