Get JSON return from a URL within the function

0

I have the following function:

$(document).ready(function(){
arrayAmount[0]='29.90';
}

I need instead of specifying the value directly there (29.90), so that it gets the value of a URL.

Ex:

arrayAmount[0]=GET:http://domain_here.com?id=1;

How can I do this?

I need something that is only one command / line, because it will be several array ..

    
asked by anonymous 01.09.2018 / 04:59

1 answer

0

First set in your javascript:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

Then use:

var parametro1 = $.urlParam('nome_do_parametro_na_url');

$.urlParam = function(name){
  var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec("http://dominio.com?id=3&city=SP");
 	return results[1] || 0;
}

var person = new Object();
person.id = $.urlParam("id");
person.city = $.urlParam("city");
console.log(person);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  Substitua <b>http://dominio.com?id=3&city=SP</b> no código acima<br>
  Por <b>window.location.href</b> para pegar a url atual
    
01.09.2018 / 06:14