jquery - 作为变量的响应文本
问题描述:
我使用下面的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