Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用fetch API处理asmx C#异常_Javascript_C#_Asmx_Fetch Api - Fatal编程技术网

Javascript 使用fetch API处理asmx C#异常

Javascript 使用fetch API处理asmx C#异常,javascript,c#,asmx,fetch-api,Javascript,C#,Asmx,Fetch Api,首先对不起我的英语 我有一个C#中的asmx,它通过客户端的fetchapi调用发送json中的数据,我以前使用过jQuery.Ajax调用,但我想开始使用fetchapi 这就是我如何进行提取调用的方式。 我调用fetchcall函数,传递url,如果需要,调用JS对象,并通过POST const jdata = await fetchcall(url, "") 然后在我的功能中,我这样做 async function fetchcall(url, data) { const PostDat

首先对不起我的英语

我有一个C#中的asmx,它通过客户端的fetchapi调用发送json中的数据,我以前使用过
jQuery.Ajax
调用,但我想开始使用fetchapi

这就是我如何进行提取调用的方式。
我调用fetchcall函数,传递url,如果需要,调用JS对象,并通过
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)
}}
error: function (xhr, textStatus, error) {
            var errorM = $.parseJSON(xhr.responseText);
            console.log(errorM.Message)
        }
这是
handleErrors
函数

function handleErrors(response) {
if (!response.ok) {
    throw Error(response.statusText);
}
return response;
}

现在我正在测试错误,但没有发送凭据,因此我得到了
会话的错误

在我的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的响应,我只会得到一个
状态文本:“Internal Server Error”
,我想要服务器响应的消息

使用jQuery.Ajax,我可以做到这一点

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)
}}
error: function (xhr, textStatus, error) {
            var errorM = $.parseJSON(xhr.responseText);
            console.log(errorM.Message)
        }
我得到了

您的会话已过期。请转到登录页并启动会话


感谢您的帮助和问候

我发现了如何在从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)

}}

如果其他人有此问题,我希望我能帮助检查您在哪里设置
会话[“uid”]
。感谢您的回复。这不是问题所在,这是客户端的错误句柄,我故意有一个会话错误,所以我可以在客户端验证错误