You asked a similar question and I recommended returning an JSON object. I also recommended a RESTful approach and provided links.
{"message" : "Hello World!"}
I recommended these approaches because they are easy to check for the existence. I left it up to you to read the docs and come up with a design of your own.
You came up with the following which I do not understand.
You can make the approach work but the it is little clunky. You'll need to loop over the results and look for the substring "Error:\n". From there it is a mystery how you want to deal with the string. Are you going to spit the string to get the message
or use the entire string with the white space character. There might be other messages with an Error:\n" substring.
I would simply return an object that is easy to handle. Maybe my initial object was too simple.
{
"isError": true,
"errorMessage": "Oops! There was an error."
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
the problem with this approach (return error as ok status) is that it only works with caught errors. url, network, or binding errors are not caught. so the ajax need to check ok static for errors, and also catch other errors. if you just return all errors
as a 500 status, you can use common logic:
Member
75 Points
513 Posts
how to show exception message from ajax
Apr 02, 2019 09:33 PM|zhyanadil.it@gmail.com|LINK
public JsonResult getColumns(String from_date, String Party_Type, String Voucher_Num, String to_date, String Account, String party, String company, String project)
{
List<String> returns = new List<string>();
List<dummy> listreg = new List<dummy>();
try
{
SqlCommand cmd = new SqlCommand("[dbo].[REPORT__General_Ledger]", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@From_Date", from_date);
cmd.Parameters.AddWithValue("@To_Date", to_date);
cmd.Parameters.AddWithValue("@Account", Account);
cmd.Parameters.AddWithValue("@party", party);
cmd.Parameters.AddWithValue("@company", company);
cmd.Parameters.AddWithValue("@project", project);
cmd.Parameters.AddWithValue("@Voucher_Num", Voucher_Num);
cmd.Parameters.AddWithValue("@Party_Type", Party_Type);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
foreach (DataColumn dr in dt.Columns)
{
listreg.Add(new dummy
{
variable = dr.ColumnName.ToString(),
});
}
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col].ToString());
}
rows.Add(row);
}
}
catch (SqlException Ex)
{
returns.Add("Error:\n" + Ex.Message);
return Json(returns, JsonRequestBehavior.AllowGet);
}
return Json(listreg, JsonRequestBehavior.AllowGet);
}
how can i show that highlight message from ajax
All-Star
53011 Points
23596 Posts
Re: how to show exception message from ajax
Apr 02, 2019 11:43 PM|mgebhard|LINK
You asked a similar question and I recommended returning an JSON object. I also recommended a RESTful approach and provided links.
I recommended these approaches because they are easy to check for the existence. I left it up to you to read the docs and come up with a design of your own.
You came up with the following which I do not understand.
You can make the approach work but the it is little clunky. You'll need to loop over the results and look for the substring "Error:\n". From there it is a mystery how you want to deal with the string. Are you going to spit the string to get the message or use the entire string with the white space character. There might be other messages with an Error:\n" substring.
I would simply return an object that is easy to handle. Maybe my initial object was too simple.
Contributor
3710 Points
1431 Posts
Re: how to show exception message from ajax
Apr 03, 2019 08:46 AM|Yuki Tao|LINK
Hi zhyanadil.it@gmail.com,
If you would like to show returns back to ajax,you could pay attention to success callback in ajax.
For example:
More details about ajax,you could refer to:
https://www.aspsnippets.com/Articles/ASPNet-MVC-Call-Controller-Method-from-View-using-jQuery-AJAX.aspx
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
58174 Points
15647 Posts
Re: how to show exception message from ajax
Apr 03, 2019 07:44 PM|bruce (sqlwork.com)|LINK
the problem with this approach (return error as ok status) is that it only works with caught errors. url, network, or binding errors are not caught. so the ajax need to check ok static for errors, and also catch other errors. if you just return all errors as a 500 status, you can use common logic:
Participant
850 Points
492 Posts
Re: how to show exception message from ajax
Apr 04, 2019 05:09 AM|AddWeb Solution|LINK
Hi, Zhyanadil.it@gmail.com
Please Refer this document, you can see lots of techniques regarding this hope you can get as you want
https://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages
Thanks