Problems Webservice ASMX

Asked

Viewed 49 times

0

When I send a JSON return and the text is accentuated, JSON breaks.


Here is the request code in the Controller

$http.post("../backend/controller/SugestaoController.asmx/comboListarSugestoes", { empresa: $rootScope.login })
    .then(function (retorno) {
        //console.log("teste", retorno.data);
        $scope.listaSugestoes = retorno.data;
    }).catch(function (retorno) {
        alert("erro");
    })
    .finally(function () {
        $scope.mostrarLoader = false;
    });

Now the Server side, I will show you just how the upload is:

            JavaScriptSerializer js = new JavaScriptSerializer();
            String json = js.Serialize(retorno);
            context.Response.Clear();
            context.Response.Charset  = "utf-8";
            context.Response.ContentType = "application/json";
            //context.Response.ContentType = "text/html";
            context.Response.AddHeader("Content-Length", (json.Length).ToString());
            context.Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            context.Response.Write(json);

If you have accents, JSON arrives like this:

[{nome:'felipe',funcao:'médico',idade:23

If he doesn’t, he’ll arrive normal

[{nome:'felipe',funcao:'medico',idade:23}]

1 answer

1


I can’t say for sure, but a guess: when informing the response size in Content-Length, use the byte size, not the number of characters in the string. This can occur because accented characters add a few bytes to the string. So instead of doing:

context.Response.AddHeader("Content-Length", (json.Length).ToString());

You do:

context.Response.AddHeader("Content-Length", Encoding.UTF8.GetBytes(json).Length.ToString());
  • I’ll try Julio, thank you.

  • It worked perfectly Julio, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.