简体   繁体   中英

Send in xmlhttp numeric data converted into a character string

I want to send numerical data through a GET retrieved via Javascript GPS position.coords.latitude here is the code.

function post() {
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      alert(xmlhttp.responseText);
    }
  }

  var v2 = -20.9008623; //position.coords.latitude;
  var v3 = 55.4958068; //position.coords.longitude;
  var v4 = v2.toString();
  var v5 = v3.toString();
  xmlhttp.open("POST", "ajax.php", true);
  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlhttp.send("LAT=" + v4 + "&LON=" + v5);
}

As you can see, var2 and var3 are numeric data and are indeed sent in this case since they are converted into a character string before being sent by the GET . On the other hand as soon as I do the recovery by position.coords.latitude nothing is sent or rather nothing is recovered by the AJAX file.

Did I forget something? Thank you for your answers.

You cant send anything else then strings with urlencoded content. Use another format.

Try with a FormData for instance.


const data = new FormData()
data.append('lat', 37)
data.append('long', 32)
xmlhttp.setRequestHeader('content-type: multipart/form-data;')
xmlhttp.send(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM