Pass a parameter from client-side to server side and get result

I knows it sounds basic but I can’t seem to get it right. I’m trying to get a data from the API but it needs a parameter in order to obtain the data. How can I pass the parameter and get the result which is a JSON array

$(function() {


	var proxy       = 'http://192.168.1.126/lms-dev-noel/proxy.php';
	var endpoint    = 'account/';
	var rt          = 'GET';
	var url         = proxy+'?endpoint='+endpoint+'&rt='+rt;
    var param       = {
        'lastsyncdate' : '2016-12-06'
    };

	$.get(url, function(param) {
	    console.log('Success');
	});


});

http://api.jquery.com/jquery.get/

You’re actually already sending parameters, namely endpoint and rt. ;-) When sending a GET request, the query string is the only way to pass along data; and if you pass a data object to $.GET as an argument, it will be converted to a query string as well. So these both calls are equivalent:

const proxy = 'http://192.168.1.126/lms-dev-noel/proxy.php'

const params = {
  endpoint: 'account/',
  rt: 'GET',
  lastsyndicate: '2016-12-06'
}

const url = proxy 
  + '?endpoint=' + params.endpoint
  + '&rt=' + params.rt 
  + '&lastsyndicate=' + params.lastysndicate

$.get(url, function (data) {
  console.log(data)
})

$.get(proxy, params, function (data) {
  console.log(data)
})

Not sure if a mixup works as well though. FWIW, I think the latter approach is much nicer as it avoids messing with string literals.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.