尝试使用 javascript 捕获页面方法
try catch the pagemethod using javascript
Javascript 将调用服务器中的 UploadData
函数,如果无法连接到该函数,它将保存到 LocalStorage。
如何在客户端捕获 pagemethod 中的错误?我尝试了以下但未触发捕获。
try{
PageMethods.UploadData(val, onlineSuccess, onlineFailed);
}catch(e)
{alert("saved in local");}
服务器端:
[System.Web.Services.WebMethod]
public static string UploadData(string text)
{
if (text == null || text.Length == 0)
return String.Empty;
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["scanner"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
string[] data = text.Split('|');
cmd.CommandText ="COMMAND HERE"
cmd.Connection = cnn;
cmd.Parameters.Clear();
.................
cnn.Open();
try
{
cmd.ExecuteNonQuery();
return data[0].ToString()+" saved";
}
catch (Exception ex)
{
return "error : "+ex.Message;
}
}
}
}
客户端调用服务端的UploadData函数:
function onlineCall(val) {
//I want to use try-catch here so when it fails to connect to
//the server will save in the local storage
PageMethods.UploadData(val, onlineSuccess, onlineFailed);
}
function onlineSuccess(res, destCtrl) {
document.getElementById("scanned_item").innerHTML =res;
}
function onlineFailed(res, destCtrl) {
alert(res);
}
您只需要使用 ajax 调用此方法并将响应写入本地存储
$.ajax({
url: '/Yourcontroller/UploadData',
type: 'GET',
data:{text:"text to pass"},
success: function (response) {
//do something with response.result
},
error: function (response) {
//write to localstorage response.result
}
});
像这样装饰控制器
[HttpGet]
public JsonResult UploadData(string text)
{
if (text == null || text.Length == 0)
return String.Empty;
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["scanner"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
string[] data = text.Split('|');
cmd.CommandText ="COMMAND HERE"
cmd.Connection = cnn;
cmd.Parameters.Clear();
.................
cnn.Open();
try
{
cmd.ExecuteNonQuery();
// return data[0].ToString()+" saved";
return new JsonResult() { result= data[0].ToString()+" saved", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
catch (Exception ex)
{
return new JsonResult() { result= ex.ToString()+" saved", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
}
}
Javascript 将调用服务器中的 UploadData
函数,如果无法连接到该函数,它将保存到 LocalStorage。
如何在客户端捕获 pagemethod 中的错误?我尝试了以下但未触发捕获。
try{
PageMethods.UploadData(val, onlineSuccess, onlineFailed);
}catch(e)
{alert("saved in local");}
服务器端:
[System.Web.Services.WebMethod]
public static string UploadData(string text)
{
if (text == null || text.Length == 0)
return String.Empty;
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["scanner"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
string[] data = text.Split('|');
cmd.CommandText ="COMMAND HERE"
cmd.Connection = cnn;
cmd.Parameters.Clear();
.................
cnn.Open();
try
{
cmd.ExecuteNonQuery();
return data[0].ToString()+" saved";
}
catch (Exception ex)
{
return "error : "+ex.Message;
}
}
}
}
客户端调用服务端的UploadData函数:
function onlineCall(val) {
//I want to use try-catch here so when it fails to connect to
//the server will save in the local storage
PageMethods.UploadData(val, onlineSuccess, onlineFailed);
}
function onlineSuccess(res, destCtrl) {
document.getElementById("scanned_item").innerHTML =res;
}
function onlineFailed(res, destCtrl) {
alert(res);
}
您只需要使用 ajax 调用此方法并将响应写入本地存储
$.ajax({
url: '/Yourcontroller/UploadData',
type: 'GET',
data:{text:"text to pass"},
success: function (response) {
//do something with response.result
},
error: function (response) {
//write to localstorage response.result
}
});
像这样装饰控制器
[HttpGet]
public JsonResult UploadData(string text)
{
if (text == null || text.Length == 0)
return String.Empty;
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["scanner"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
string[] data = text.Split('|');
cmd.CommandText ="COMMAND HERE"
cmd.Connection = cnn;
cmd.Parameters.Clear();
.................
cnn.Open();
try
{
cmd.ExecuteNonQuery();
// return data[0].ToString()+" saved";
return new JsonResult() { result= data[0].ToString()+" saved", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
catch (Exception ex)
{
return new JsonResult() { result= ex.ToString()+" saved", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
}
}