Jquery ajax post without form elements

I need to send data, and the url is just “v1 / wave”, and it takes five parameters. I tried this, but it is not working yet:

function request(minLat, minLon, maxLat, maxLon, maxNrOfResults, callback){ $.ajax({ url: 'v1/wave?minLat='+minLat+'&minLong='+minLon+'&maxLat='+maxLat+'&maxLong='+maxLong'+&maxNrOfResults='+maxNrOfResults, type: "GET", success: function (data) { callback(data); if(data.msgCode == LOGIN_SUCCESS){ console.log("request success"); } else if(data.msgCode == LOGIN_FAILED){ console.log("request failed"); } }, error: function(data) { handleRequestError(data); } }) 

ERROR: Uncaught SyntaxError: Unexpected string in URL string.

+4
source share
3 answers

You should avoid sending parameters to URLs. You must use the data property. There are several advantages, including coding .. or typos :)

 function request(minLat, minLon, maxLat, maxLon, maxNrOfResults, callback){ $.ajax({ url: 'v1/wave', data: { minLat : minLat, minLong : minLong, maxLat : maxLat, maxLong : maxLong, maxNrOfResults : maxNrOfResults }, type: "POST", success: function (data) { callback(data); if(data.msgCode == LOGIN_SUCCESS){ console.log("request success"); } else if(data.msgCode == LOGIN_FAILED){ console.log("request failed"); } }, error: function(data) { handleRequestError(data); } }) 
+6
source

There is a typo here: '&maxLong='+maxLong'+

+2
source

Try below

If you want to publish the data, you can move your data from the query string to publish the data for a more secure one. Also use ajax type POST

 var myPostData=JSON.stringify({'minLat':minLat,'minLong':minLon, etc}); $.ajax({ url: 'v1/wave', type: "POST", data:myPostData, dataType: "json", success: function (data) { callback(data); if(data.msgCode == LOGIN_SUCCESS){ console.log("request success"); } else if(data.msgCode == LOGIN_FAILED){ console.log("request failed"); } }, error: function(data) { handleRequestError(data); } }) 
+2
source

Source: https://habr.com/ru/post/1440245/


All Articles