0

I have some javascript code which uses jQuery to call $.post(). I want to send a string parameter to that function, but my attempts have been unsuccessful. I tried this function call I saw on another post, but it didn't work $.post(SavePoint, { data });

myTimer = (function(ctx) {
  $.post(getPoint).done(function(xml) {
    parseXml(xml);

    var lon = (parseFloat($xml.find("lon").text()) + 180) * (screen.height / 360);
    var lat = (parseFloat($xml.find("lat").text()) + 90) * (screen.width / 180);

    draw(ctx, rout, lat, lon);

    if (recordTime <= 0 && recordTime > -0.2) {
      $.post(SaveToFile);
      isSaveNeeded = "false";
    }

    var isSaveNeeded = document.getElementById("isSaveNeeded").value;
    if (isSaveNeeded == "true") {
      var latS = parseFloat($xml.find("lat").text());
      var lonS = parseFloat($xml.find("lon").text());
      var rudderS = parseFloat($xml.find("rudder").text());
      var throttleS = parseFloat($xml.find("throttle").text());

      var data = latS + "," + lonS + "," + rudderS + "," + throttleS + ",";      
      alert(data);

      $.post(SavePoint, {
        data
      }); //This func call doesn't work..

      alert(data);
      recordTime -= 0.25;
    }
  });
});

setInterval(function() {
  myTimer(ctx);
}, 1000 / rate);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

0

$.post take the second param as object not string, I think your data is string, take reference from following. In SavePoint endpoint you will gate data params.

var data = latS + "," + lonS + "," + rudderS + "," + throttleS + ",";
alert(data);
$.post(SavePoint, { data: data }, 
    function(returnedData){
         console.log(returnedData);
}).fail(function(){
      console.log("error");
});
Paresh Barad
  • 1,544
  • 11
  • 18
  • Where do console.log prints? I tried to use your post decleration but it didn't work as well.. – Oded Ben-Noon May 30 '19 at 10:59
  • @OdedBen-Noon You can define `$.post` 2 ways first with callback and second without a callback, I defined with callback, You can also define as normal If you don't want to use the result, normal post look like `$.post(SavePoint, { data: data })` – Paresh Barad May 30 '19 at 11:03