从jquery ajax成功中获取价值 [英] get value from jquery ajax success
本文介绍了从jquery ajax成功中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在对控制器进行ajax调用并从控制器返回一个字符串值。
控制器中的
:
Hi,
I am making an ajax call to controller and returning a string value from controller.
in controller:
public JsonResult ()
{
// some service call to get data
string output = some value from service call;
return Json(output);
}
javascript中的
:
in javascript:
var output = "";
$.ajax({
url: '/controller/action',
traditional: true,
success: function (result) {
output = result;
},
error: function failCallBk(XMLHttpRequest, textStatus, errorThrown) {
alert("An error occurred while processing your request. Please try again.");
}
});
我需要从成功事件中获得结果。有没有办法?
I need to get result from the success event. Is there any possible way?
推荐答案
.ajax({
url:' / controller / action',
traditional: true ,
成功: function (result){
output = result;
},
error: function failCallBk(XMLHttpRequest,textStatus,errorThrown){
alert( 处理您的请求时出错。请尝试再次。);
}
});
.ajax({ url: '/controller/action', traditional: true, success: function (result) { output = result; }, error: function failCallBk(XMLHttpRequest, textStatus, errorThrown) { alert("An error occurred while processing your request. Please try again."); } });
我需要从成功事件中获得结果。有没有办法?
I need to get result from the success event. Is there any possible way?
您必须更改您的代码如下:
控制器
You have to change your code is as below:
Controller
[HttpGet]
public JsonResult GetOutput()
{
// some service call to get data
string output = some value from service call;
return Json(output,JsonRequestBehavior.AllowGet);
}
javascript
javascript
var output = "";
.ajax({
url:/ controller / GetOutput,
类型:'获取',
dataType:json,
成功:函数(结果){
输出=结果;
},
错误:函数failCallBk( XMLHttpRequest,textStatus,errorThrown){
alert(处理您的请求时出错。请再试一次。);
}
});
.ajax({ url: "/controller/GetOutput", type: 'Get', dataType: "json", success: function (result) { output = result; }, error: function failCallBk(XMLHttpRequest, textStatus, errorThrown) { alert("An error occurred while processing your request. Please try again."); } });
我希望这会对你有所帮助。
I hope this will help to you.
这篇关于从jquery ajax成功中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文