Parameter arriving truncated way in Ajax request

Asked

Viewed 196 times

1

I have a requisition Ajax that gets the data I need from a WebMethod. The problem is that the parameter used for the request is "coming" truncated in the WebMethod the length of the parameter is 44 digits.

Sent parameter: 31161102996615000145550000000413281487877818.

How it’s coming: 3.1161102996615E+43

Ajax code:

var valorCampo= $(this).attr('data-clipboard-text');
var content = "parametroConsulta=" + encodeURIComponent(valorCampo);

$.ajax({

        type: "GET",
        dataType: 'json',
        async: false,
        contentType: "application/json; charset=utf-8",
        url: "myPage.aspx/ObtenhaDados",
        data: content,
        success: function (data) { dadosObtidos(data); },
        error: function (data) { falhaConsulta(data); }

});

WebMethod:

 [WebMethod]
 [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
 public static DadosPessoais ObtenhaDados(string valoPesquisa)
 { .... }

Url displayed in Firebug:

http://localhost:61187/ps/myPage.aspx/ObtenhaDados?valoPesquisa=31161102996615000145550000000413281487877818

  • content in the Ajax request is a string? Try using number to content and float for signature of the method ObtenhaDados

  • Yes content is a string...

  • Try to change the shipping method in jQuery to "POST", so: var content = "{ parametroConsulta: " + encedURIComponent(valorCampo) + " }"

  • Too large a number, it is being converted to a readable basis. If you give a alert(31161102996615000145550000000413281487877818); the exit will be 3.1161102996615E+43. It is being converted to scientific notation.

  • That’s right, that’s what I’m trying to figure out, so he’s like string rsrsrsrsr

  • With this amount of number I don’t think it works. What you could use with a smaller amount of number would be Number and toPrecision or toFixed. 44 digits is a lot, even for a computer.

Show 1 more comment

2 answers

1

I was able to solve the problem by placing the parameter between "'". Code below:

var valorCampo = $(this).attr('data-clipboard-text');
var content = {valorPesquisa: "'" + valorCampo + "'"};

0

This way you will pass the value as a string and concatenate with the parametroConsult, but you could do this in c#

var valorCampo= $(this).attr('data-clipboard-text');
var content = "parametroConsulta=" + valorCampo.toString();

$.ajax({

        type: "GET",
        dataType: 'json',
        async: false,
        contentType: "application/json; charset=utf-8",
        url: "myPage.aspx/ObtenhaDados",
        data: content,
        success: function (data) { dadosObtidos(data); },
        error: function (data) { falhaConsulta(data); }

});
  • Only one thing, in case it already comes as string when you grab the attr. Just check using typeof.

  • the toString() unmolested... :(

  • @Jcsaint you are sending as String and receiving as string ?

  • Sending as string and I get a Json

Browser other questions tagged

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