从javascript将大表单号转换为字符串

从javascript将大表单号转换为字符串,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个表单可以查找DB2技术id,技术id是一个26位数字。我想通过ajax将其作为字符串传递给我的后端进程,但每次转换时: 2.015052714252E+25 这会破坏后端代码 我想通过使用toString函数可以解决这个问题,但运气不好。以下是jquery部分: $('form').submit(function(event) { var id = $('#tech_id').val().toString(); // the form input with the tech_id $.a

我有一个表单可以查找DB2技术id,技术id是一个26位数字。我想通过ajax将其作为字符串传递给我的后端进程,但每次转换时:

2.015052714252E+25

这会破坏后端代码

我想通过使用
toString
函数可以解决这个问题,但运气不好。以下是jquery部分:

$('form').submit(function(event) {
var id = $('#tech_id').val().toString(); // the form input with the tech_id
$.ajax({
    type: 'POST',
    url: 'do_stuff',
    data: {id: id}, // also tried data: {id: id.toString()} here
    dataType: 'json',
    encode: true
})
然后在后端,我像这样传入它,再次尝试将其转换为字符串:

    $techid = $content['id']; // from the PHP $_POST array
    $host = 'my_API_endpoint'; 

    $url = $host."/user/".$techid;

    $results = file_get_contents((string) $url);
我不断得到以下错误:

file_get_contents(http:my_api_endpoint/user/2.015052714252E+25): failed to open stream: HTTP request failed! HTTP\/1.0 500 Internal Server Error
知道有什么问题吗


编辑:我把它缩小到控制器中的表单处理程序,它自动
json\u解码所有输入。转换字符串的是
json\u decode
函数(在php中)

JavaScript没有BigInteger类型,因此它转换为指数形式。基本上,为了处理这类案件,人们使用了很多技术

也有很多库来处理这个问题。比如,

但对于您的情况,据我所知,我建议尝试将26位数字转换为十六进制并发送到后端。在后端,再次将其从十六进制转换回整数

我的意思是试着用这个

 $('form').submit(function(event) {
    var id = $('#tech_id').val().toString(16); // this converts to hexa
 $.ajax({
     type: 'POST',
     url: 'do_stuff',
     data: {id: id}, // this will be a String in hexa format.
     dataType: 'json',
     encode: true
})

JavaScript没有BigInteger类型,因此它转换为指数形式。基本上,为了处理这类案件,人们使用了很多技术

也有很多库来处理这个问题。比如,

但对于您的情况,据我所知,我建议尝试将26位数字转换为十六进制并发送到后端。在后端,再次将其从十六进制转换回整数

我的意思是试着用这个

 $('form').submit(function(event) {
    var id = $('#tech_id').val().toString(16); // this converts to hexa
 $.ajax({
     type: 'POST',
     url: 'do_stuff',
     data: {id: id}, // this will be a String in hexa format.
     dataType: 'json',
     encode: true
})
然后在查询时将其转换为数字,或者不需要将其转换为数字,您可以将其保留为字符串,没有问题


然后在查询时将其转换为数字,或者不需要将其转换为数字,您可以将其保留为字符串,没有问题

使用url\u编码如下

 $url = $host."/user/".$techid;
 $url=  url_encode($url);

现在使用文件获取内容可能会有帮助

使用url\u这样编码

 $url = $host."/user/".$techid;
 $url=  url_encode($url);

现在使用file get content可以帮助我们查看
var\u dump($\u POST)
,以及该值如何从
$\u POST
$content
。是的,因为我已经尝试了你的代码,它对我有用。将值设置为tech\u id时添加eval函数意味着它将打印所有的数字显示
var\u dump($\u POST)
以及该值从
$\u POST
$content
的获取方式。是的,因为我已经尝试了你的代码,它对我有效。将值设置为tech\u id时添加eval函数意味着它将打印所有数字。我想将其作为字符串而不是整数传递。是否只希望字符串为十进制格式?实际上.toString(16)返回一个十六进制格式的字符串。我想将其作为字符串而不是整数传递。是否只希望字符串为十进制格式?实际上,toString(16)返回一个十六进制格式的字符串。