提取/承诺-返回序列化DataTable的ASPX方法(Newtonsoft)-返回Json对象的数组

时间:2018-11-16 18:18:24

标签: javascript c# asp.net json fetch

我在C#.ASMX上有一个Web服务。

在此类中,我有以下方法:

var cadastro = user.FirstOrDefault();
const string Issuer = "adminScheme";

List<Claim> claims = new List<Claim>
{
    new Claim(ClaimTypes.Name, cadastro.NomeUsuario, ClaimValueTypes.String, Issuer),
    new Claim("Idusuario",cadastro.Id.ToString(), ClaimValueTypes.String, Issuer),
    new Claim("IdtipoUsuario", cadastro.IdtipoUsuario.ToString(), ClaimValueTypes.String, Issuer)
};

ClaimsIdentity identity = new ClaimsIdentity(claims, "cookie");

ClaimsPrincipal principal = new ClaimsPrincipal(identity);

await HttpContext.SignInAsync(scheme: Issuer,
        principal: principal,
        properties: new AuthenticationProperties
        {
            IsPersistent = true,
            ExpiresUtc = DateTime.UtcNow.AddMinutes(480)
        });

return RedirectToLocal(returnUrl);

该方法工作正常...但是当我尝试使用Javascript上的访存/保证来获取此响应时,出现以下错误:

错误语法错误:JSON输入意外结束

JS代码:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void getJson()
{
    DataTable data = "SELECT * FROM AT_MasterData Order by [Order]".fwSqlFillDataTable();

    string strResponse = JsonConvert.SerializeObject(data);

    Context.Response.Clear();
    Context.Response.ContentType = "application/json; charset=utf-8";
    Context.Response.AddHeader("content-length", strResponse.Length.ToString());
    Context.Response.Flush();

    Context.Response.Write(strResponse);
}

如果我保证将.json()更改为.text(),我将得到结果...

JS工作代码:

function getJson(sectionActual) {
    let url = './services/assessment.asmx/getJson';
    let headers = new Headers();

    headers.append('Accept', 'application/json'); // This one is enough for GET requests

    fetch(url,
        {
            method: "POST", 
            headers: headers
        })
        .then(result => result.json())
        .then(resultData => {
            console.log(resultData)
        })
        .catch(err => console.log("Error", err));

}

控制台的屏幕: enter image description here

是否存在一种解决方法,可以使用promise .json()自动转换数据?

我想问题是方法不返回JSON结构...返回带有JSON数据的数组(将数组中JSON对象中的每个项目数据行拆分)

1 个答案:

答案 0 :(得分:0)

解决方案非常简单... 标题行只包含+ 1:

public function fcmtoken_referesh(Request $request){
        $postArray = ['fcm_token' => $request->fcm_token];
        $login = Employee::where('api_token',$request->api_token)->update($postArray);

        if ($login) {

            return 'true';

        }else{

            return 'false';

        }

    }  

但是,我无法确定将+1添加到内容长度中的原因。

Context.Response.AddHeader("content-length", (strResponse.Length + 1).ToString());

如果我尝试返回转换为JSON的通用类,则需要删除此+1(以下代码不会将内容返回给JS):

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void getJson()
{
    DataTable data = "SELECT * FROM AT_MasterData Order by [Order]".fwSqlFillDataTable();

    string strResponse = JsonConvert.SerializeObject(data);

    Context.Response.Clear();
    Context.Response.ContentType = "application/json; charset=utf-8";
    Context.Response.AddHeader("content-length", (strResponse.Length + 1).ToString());
    Context.Response.Flush();

    Context.Response.Write(strResponse);
}

此承诺无法用strResponse = JsonConvert.SerializeObject(new { test = "Hello World" }); Context.Response.Clear(); Context.Response.ContentType = "application/json; charset=utf-8"; Context.Response.AddHeader("content-length", (strResponse.Length + 1).ToString()); Context.Response.Flush(); Context.Response.Write(strResponse); .json()来解决-两者都不起作用...

但是,如果我从内容长度中删除+1 ...可以:

.text()

结果:

enter image description here