[英]Handle asmx C# Exception with fetch API
First of all sorry for my English... 首先,对不起我的英语...
I have an asmx in C# that send data in json with an fetch API Call in the client side, i was using the jQuery.Ajax
call before, but i want to start using fetch API. 我在C#中有一个asmx,它在客户端使用fetch API调用在json中发送数据,我以前使用过
jQuery.Ajax
调用,但是我想开始使用fetch API。
This is how i do the Fetch Call. 这就是我进行提取呼叫的方式。
I call the function fetchcall passing the url and if is needed the JS object with the parameters to be send by POST
我通过传递URL来调用函数fetchcall,如果需要的话,JS对象以及要通过
POST
发送的参数
const jdata = await fetchcall(url, "")
Then in my function i do this 然后在我的职能中我这样做
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)
}}
And this is the handleErrors
function 这是
handleErrors
函数
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
} }
Right now i am testing the error without sending the credentials, so i get a error for the Session
现在我正在测试错误而不发送凭据,所以我在
Session
遇到了错误
And in my C# asmx i have this 在我的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");}
With that the web services is throwing me an error with the message of Your session has expired please go to login page and start session Web服务向我抛出错误,提示您您的会话已过期,请转到登录页面并开始会话
But wen i check the response of the fetch API in the handleError function i only get a statusText:"Internal Server Error"
and i want the message that the server respond. 但是,当我在handleError函数中检查提取API的响应时,我只得到一个
statusText:"Internal Server Error"
并且我希望服务器响应的消息。
With jQuery.Ajax i do this 使用jQuery.Ajax我做到了
error: function (xhr, textStatus, error) {
var errorM = $.parseJSON(xhr.responseText);
console.log(errorM.Message)
}
And i get the 我得到了
Your session has expired please go to login page and start session
您的会话已过期,请转到登录页面并开始会话
Thank you for the help and regards 谢谢您的帮助和问候
I discovered how to handle the error correctly if an exception is sent from C# 我发现如果从C#发送异常,如何正确处理错误
I removed the handleErrors
function and check the response inside the fetchcall
so if the response.ok
is false
then i check the json data that the server respond and get the json.Message
. 我删除了
handleErrors
功能,检查里面的响应fetchcall
所以如果response.ok
是false
,然后我检查JSON数据的服务器响应,并获得json.Message
。
This is the end code 这是结束码
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)
}}
In case someone else has this problem I hope I can help 万一别人有这个问题,我希望我能帮忙
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.