使用 fetch API 处理 asmx C# 异常
Handle asmx C# Exception with fetch API
首先对不起我的英语...
我在 C# 中有一个 asmx,它在 json 中发送数据并在客户端调用 API,我之前使用的是 jQuery.Ajax
调用,但我想开始使用 fetch API.
这就是我执行 Fetch Call 的方式。
我调用传递 url 的函数 fetchcall,如果需要 POST
发送带有参数的 JS 对象
const jdata = await fetchcall(url, "")
然后在我的函数中我这样做
async function fetchcall(url, data) {
const PostData = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
dataType: 'json'
//credentials: 'include'
}
if (data) { PostData.body = JSON.stringify(data) }
try {
const response = await fetch(url, PostData)
const json = await (handleErrors(response)).json();
//This is a temporary solution to the problem
if (json == 'Su sesion ha expirado favor de ir a pagina de login e iniciar session') {
alert(json);
return false;
}
return json
} catch (e) {
console.log(e)
}}
这是handleErrors
函数
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
现在我正在测试错误但没有发送凭据,所以我得到了 Session
的错误
在我的 C# asmx 中我有这个
[WebMethod(Description = "Load Countries", EnableSession = true)]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string fillcbocountries()
{
var authCookie = Session["uid"];
if (authCookie == null)
throw new Exception("Your session has expired please go to login page and start session");}
因此,Web 服务向我抛出错误消息“您的会话已过期,请转到登录页面并开始会话”
但是当我在 handleError 函数中检查 fetch API 的响应时,我只得到一个 statusText:"Internal Server Error"
并且我想要服务器响应的消息。
jQuery.Ajax我这样做
error: function (xhr, textStatus, error) {
var errorM = $.parseJSON(xhr.responseText);
console.log(errorM.Message)
}
我得到
Your session has expired please go to login page and start session
感谢您的帮助和问候
我发现了如果从 C# 发送异常时如何正确处理错误
我删除了 handleErrors
函数并检查 fetchcall
中的响应
所以如果 response.ok
是 false
那么我检查服务器响应的 json 数据并获得 json.Message
。
这是结束码
async function fetchcall(url, data) {
const PostData = {
method: 'POST',
headers: {
"Accept": "application/json",
'Content-Type': 'application/json; charset=utf-8'
},
dataType: 'json',
credentials: 'include'
}
if (data) { PostData.body = JSON.stringify(data) }
try {
//const url = 'services/common.asmx/fillcbopais'
const response = await fetch(url, PostData)
const jdata = await response.json();
if (!response.ok) {
alert(jdata.Message);
throw Error(jdata.Message)
}
return json
} catch (e) {
console.log(e)
}}
万一其他人有这个问题我希望我能帮助
首先对不起我的英语...
我在 C# 中有一个 asmx,它在 json 中发送数据并在客户端调用 API,我之前使用的是 jQuery.Ajax
调用,但我想开始使用 fetch API.
这就是我执行 Fetch Call 的方式。
我调用传递 url 的函数 fetchcall,如果需要 POST
const jdata = await fetchcall(url, "")
然后在我的函数中我这样做
async function fetchcall(url, data) {
const PostData = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
dataType: 'json'
//credentials: 'include'
}
if (data) { PostData.body = JSON.stringify(data) }
try {
const response = await fetch(url, PostData)
const json = await (handleErrors(response)).json();
//This is a temporary solution to the problem
if (json == 'Su sesion ha expirado favor de ir a pagina de login e iniciar session') {
alert(json);
return false;
}
return json
} catch (e) {
console.log(e)
}}
这是handleErrors
函数
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
现在我正在测试错误但没有发送凭据,所以我得到了 Session
在我的 C# asmx 中我有这个
[WebMethod(Description = "Load Countries", EnableSession = true)]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string fillcbocountries()
{
var authCookie = Session["uid"];
if (authCookie == null)
throw new Exception("Your session has expired please go to login page and start session");}
因此,Web 服务向我抛出错误消息“您的会话已过期,请转到登录页面并开始会话”
但是当我在 handleError 函数中检查 fetch API 的响应时,我只得到一个 statusText:"Internal Server Error"
并且我想要服务器响应的消息。
jQuery.Ajax我这样做
error: function (xhr, textStatus, error) {
var errorM = $.parseJSON(xhr.responseText);
console.log(errorM.Message)
}
我得到
Your session has expired please go to login page and start session
感谢您的帮助和问候
我发现了如果从 C# 发送异常时如何正确处理错误
我删除了 handleErrors
函数并检查 fetchcall
中的响应
所以如果 response.ok
是 false
那么我检查服务器响应的 json 数据并获得 json.Message
。
这是结束码
async function fetchcall(url, data) {
const PostData = {
method: 'POST',
headers: {
"Accept": "application/json",
'Content-Type': 'application/json; charset=utf-8'
},
dataType: 'json',
credentials: 'include'
}
if (data) { PostData.body = JSON.stringify(data) }
try {
//const url = 'services/common.asmx/fillcbopais'
const response = await fetch(url, PostData)
const jdata = await response.json();
if (!response.ok) {
alert(jdata.Message);
throw Error(jdata.Message)
}
return json
} catch (e) {
console.log(e)
}}
万一其他人有这个问题我希望我能帮助