jQuery Ajax:返回值给调用者?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/388436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Ajax: return value to caller?
提问by venkatachalam
I have some jQuery code. I have called an Ajax function file, file.php, that has some fields, like:
我有一些 jQuery 代码。我调用了一个 Ajax 函数文件 file.php,它有一些字段,例如:
<input type="radio" value="plz">Milk</input>.
Will I assign into again jQuery function? If so, how do I do it? I attached a sample file:
我会再次分配给 jQuery 函数吗?如果是这样,我该怎么做?我附上了一个示例文件:
<html>
<head>
<LINK REL=StyleSheet HREF="examples.css" TITLE="Contemporary" TYPE="text/css">
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script src="jquery-impromptu.1.6.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$.ajax({
type:"GET",
url:"file.php",
data:id,
success:function(){
var txt=id;
$.prompt( txt,{ opacity: 0.2 });
},
error:function(){
window.location("ERRoR");
}
});
});
</script>
<body>
</body>
</html>
回答by Athena
The success
function takes a parameter, which contains the fetched data. So in your example:
该success
函数采用一个参数,该参数包含获取的数据。所以在你的例子中:
$(document).ready(
function(){ $.ajax({
type:"GET",
url:"file.php",
data:id,
success:function(txt){
$.prompt( txt,{ opacity: 0.2 });
},
// ... more ...
}
});
More examples are in the jQuery documentation.
更多示例在jQuery 文档中。
回答by smitha kamath
jQuery.ajax({
type:"POST",
url:"file.php",
data:"id1="+val1+"&id2="+val2,
success:function(data){
jQuery("#div_id").html(data);
}