I have a simple script like this:
request = $.ajax({
url: "/getmesomefloats.php",
type: "post",
});
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log(response, textStatus);
if(textStatus == 'success') {
console.log($.parseJSON(response);
}
});
Where getmesomefloats.php looks like this:
$points[] = array(-14.27664,-170.6878);
$points[] = array(-16.29323,-165.3741);
$points[] = array(-15.86157,-162.7885);
$points[] = array(-15.89847,-160.2066);
echo json_encode($all_points);
the first console log call spits out this string:
[[-14.27664,-170.6878],[-16.29323,-165.3741],[-15.86157,-162.7885],[-15.89847,-160.2066]] success
The $.parseJSON (or JSON.parse(response)) spits out this ("expanded" in the console):
[Array[2], Array[2], Array[2], Array[2]]
0: Array[2]
0: -1589268.2950388812
1: NaN
length: 2
__proto__: Array[0]
1: Array[2]
0: -1813754.066977689
1: NaN
length: 2
__proto__: Array[0]
2: Array[2]
0: -1765701.8955818643
1: NaN
length: 2
__proto__: Array[0]
3: Array[2]
0: -1769809.5847921362
1: NaN
length: 2
__proto__: Array[0]
length: 4
__proto__: Array[0]
I don't understand why I get the NaN's and why eg -14.27664 is converted to -1589268.2950388812 ? What should I do to get the correct values? Are the floating point numbers to much to handle for javascript? Bonus info: The floats are GPS coordinates, and I ('m going to) use them in a map script not included here.
I have a simple script like this:
request = $.ajax({
url: "/getmesomefloats.php",
type: "post",
});
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log(response, textStatus);
if(textStatus == 'success') {
console.log($.parseJSON(response);
}
});
Where getmesomefloats.php looks like this:
$points[] = array(-14.27664,-170.6878);
$points[] = array(-16.29323,-165.3741);
$points[] = array(-15.86157,-162.7885);
$points[] = array(-15.89847,-160.2066);
echo json_encode($all_points);
the first console log call spits out this string:
[[-14.27664,-170.6878],[-16.29323,-165.3741],[-15.86157,-162.7885],[-15.89847,-160.2066]] success
The $.parseJSON (or JSON.parse(response)) spits out this ("expanded" in the console):
[Array[2], Array[2], Array[2], Array[2]]
0: Array[2]
0: -1589268.2950388812
1: NaN
length: 2
__proto__: Array[0]
1: Array[2]
0: -1813754.066977689
1: NaN
length: 2
__proto__: Array[0]
2: Array[2]
0: -1765701.8955818643
1: NaN
length: 2
__proto__: Array[0]
3: Array[2]
0: -1769809.5847921362
1: NaN
length: 2
__proto__: Array[0]
length: 4
__proto__: Array[0]
I don't understand why I get the NaN's and why eg -14.27664 is converted to -1589268.2950388812 ? What should I do to get the correct values? Are the floating point numbers to much to handle for javascript? Bonus info: The floats are GPS coordinates, and I ('m going to) use them in a map script not included here.
Share Improve this question asked Apr 26, 2015 at 23:21 Anders IversenAnders Iversen 231 silver badge7 bronze badges 4- Why not put them in a string i am not sure what's is causing this. Because i just strings for floating points – Bob Thomas Commented Apr 26, 2015 at 23:25
- There is no reason why there would be any problem to handle floating point values, JavaScript uses double precision floating point numbers for all numbers. When I try to parse that string using JSON.parse in Firefox, it works just fine. What brower are you using? – Guffa Commented Apr 26, 2015 at 23:36
- Hi Guffa and Bob, I'm using Chrome. Bob, If I were to use the string would you suggest that I write some custom function to parse the returned array? Could the error be browser related? – Anders Iversen Commented Apr 27, 2015 at 11:08
- 1 Hi All, I am facing the similar issue with negative floating point value in JSON – IfOnly Commented Oct 5, 2015 at 10:56
2 Answers
Reset to default 3I had an issue with parsing negative values from some data I copy/pasted from wikipedia. It turned out that the minus sign was the wrong character (ascii decimal 150 instead of 45).
There is nothing in [[-14.27664,-170.6878],[-16.29323,-165.3741],[-15.86157,-162.7885],[-15.89847,-160.2066]]
given that JavaScript can't handle.
Maybe you could try including the raw output from your request in case there is something I can't see in the console.log output you gave.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745149996a4613824.html
评论列表(0条)