-1

I have an API which I'd like to call through client-side JavaScript. I need to parse a URL parameter ref which then needs to be passed to the API call. For example:

I visit this page www.example.com?ref=123, JavaScript parses ref and passes it to /api/v1.0/add?ref=123 and sends the request.

How can i do this?

Edit - Other answers show parsing the URL not making the API call.

Adders
  • 665
  • 8
  • 29
  • You [read the query string](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) and append it to the query string your forwarding, – Liam Jul 03 '17 at 09:55
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Liam Jul 03 '17 at 09:55
  • Please see https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters – U.P Jul 03 '17 at 09:55

2 Answers2

1

You can do something like this:

var url_string = "http://www.example.com?ref=123";
var url = new URL(url_string);
var ref = url.searchParams.get("ref");
var urlGet = '/api/v1.0/add?ref=' + ref

$.get( urlGet , function( data ) {   

});

André

andrescpacheco
  • 604
  • 1
  • 8
  • 26
0

For that you may use .get() function, as follows:

var input = 123;
var url = "/api/v1.0/add?ref="+input;
$.get( url, function( data ) {   

       // data is the return value, if any. You may use this return value, data, over here 

    });
Vagabond
  • 877
  • 1
  • 10
  • 23