Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax POST without form elements

Tags:

jquery

ajax

I need to post data and the url is simply "v1/wave" and it takes five params. I tried this, but it doesn't work so far:

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 the url line.

like image 743
MJB Avatar asked Jan 16 '23 07:01

MJB


2 Answers

You should avoid sending the parameters in the url. You should use the data property. There are several advantages, including encoding .. 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);
            }
        })
like image 160
Gabriel Diaconescu Avatar answered Jan 26 '23 03:01

Gabriel Diaconescu


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

like image 36
CaptainCarl Avatar answered Jan 26 '23 04:01

CaptainCarl