我想将结果设置为来自 MVC Controller 方法的变量。
function someFunction(){
var result;
$.Ajax{
//???
}
return result;
}
//与c++相比
int f() { //just! return result; }
P.S.: 它不是异步的,它应该是同步函数调用,以便将值返回给函数体。
最佳答案
设置 Controller
public ActionResult Foo(){
return Content("Hello Anton");
}
设置ajax请求,记得设置async:false
,默认为true,以便返回值
function greeting(){
var x= $.ajax({
url:'/ControllerName/Foo',
async:false,
});
return x.responseText();
}
现在你可以调用这个函数了
var v = greeting();
alert(v); //if every thing goes well you will see "Hello Anton"
关于javascript - Ajax 。我怎样才能返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034930/