我使用下面的jQuery代碼在div中插入responsetext。jquery - 作爲變量的響應文本
$("#div").load('page.php?val='+myvalue)
是否有可能將responsetext轉換爲javascript變量而不是將其應用於div。
我使用下面的jQuery代碼在div中插入responsetext。jquery - 作爲變量的響應文本
$("#div").load('page.php?val='+myvalue)
是否有可能將responsetext轉換爲javascript變量而不是將其應用於div。
例如,您需要用.get()
替換。謹慎的
$.get('page.php?val='+myvalue, function(data) {
console.log(data);
// you might want to "store" the result in another variable here
});
一個字:在上面的代碼片段的data
參數不necesarilly墊片從墊層XHR對象responseText
屬性,但它會包含請求返回什麼。如果你需要的是直接訪問,你可以這樣調用它
$.get('page.php?val='+myvalue, function(data, status, jXHR) {
console.log(jXHR.responseText);
});
是的,你應該使用$不用彷徨方法:
var variable = null;
$.get('page.php?val='+myvalue,function(response){
variable = response;
});
更多關於此命令:http://api.jquery.com/jQuery.get/
還有一個$。員額和$就命令
嘗試是這樣的:
var request = $.ajax({
url: "/ajax-url/",
type: "GET",
dataType: "html"
});
request.done(function(msg) {
// here, msg will contain your response.
});
定義就像一個回調函數:
$("#div").load('page.php?val='+myvalue, function(responseText) {
myVar = responseText;
});
注意的每個人都在說使用$.get
- 你不需要待辦事項這一點,將正常工作如上。
.load(url [, data] [, complete(responseText, textStatus, XMLHttpRequest)])
我可以做這樣的事情警報(執行console.log(jXHR.responseText)); – ravi404 2012-01-04 12:05:29
如何獲取字符串的值? – ravi404 2012-01-04 12:05:49
@ravz:是的,'jXHR.responseText'應該包含響應字符串。 – jAndy 2012-01-04 12:09:39