如何在ASP.NET MVC中使用ajax调用后发回字符串消息 [英] How to return string message after post using ajax call in ASP.NET MVC
问题描述
您好,我有这个小问题。
当我们使用ajax发布时,我可以在成功后使用控制器方法中的ActionResult类型返回布尔类型的值,但我不能返回像成功或其他消息的字符串。你能告诉我怎么办这样的东西?
任何帮助都会受到赞赏。
我尝试了什么:
我试过这样的东西
这适用于jquery中的ajax调用
Hi there, i have this small question.
When we do post using ajax, i can return boolean type of value using ActionResult type in controller method after success but I cannot return string like 'Success' or other message. Can you guys tell me how can i do that kind of stuff?
Any help would be appreciated.
What I have tried:
I tried something like this
this goes for ajax call in jquery
$.ajax({
url: "/Employee/SubmitInformation",
dataType: 'json',
data: {
EmployeeID: EmpID,
EmpName: empName
},
type: "POST",
cache: false,
success: function (data) {
//want to return data as not boolean but string here
},
error: function (a, b, c) {
alert("Error")
}
});
这是控制器中的
this goes in controller
[HttpPost]
public ActionResult SubmitInformation(int EmployeeID,string EmpName)
{
//after successful entry of information
//return success message
return Json("success", JsonRequestBehavior.AllowGet);
}
推荐答案
.ajax({
url:/ Employee / SubmitInformation,
dataType:'json',
data:{
EmployeeID:EmpID,
EmpName:empName
},
type:POST ,
cache:false,
success:function(data){
//想要返回数据不是布尔值而是字符串这里
},
错误:函数(a,b,c){
alert(Error)
}
});
.ajax({ url: "/Employee/SubmitInformation", dataType: 'json', data: { EmployeeID: EmpID, EmpName: empName }, type: "POST", cache: false, success: function (data) { //want to return data as not boolean but string here }, error: function (a, b, c) { alert("Error") } });
这是控制器中的
this goes in controller
[HttpPost]
public ActionResult SubmitInformation(int EmployeeID,string EmpName)
{
//after successful entry of information
//return success message
return Json("success", JsonRequestBehavior.AllowGet);
}
如果你想只返回一个字符串文字,那么你可以使用 Content():
If you want to just return a string literal, in that case you can use Content() :
return Content("success");
并在javascript中检查响应:
and in javascript check the reponse:
success: function (data) {
if(data == "success")
}
这篇关于如何在ASP.NET MVC中使用ajax调用后发回字符串消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!