How to get real value of parameters in url - javascript

I have problem but I tried many solutions but it does not work .
Here is my code:
var params = '?DepartmentId=' + DepartmentId + '&DepartmentName=' + DepartmentName;
When I send params 'DepartmentName' with value 'R&D ABC' It only send 'R' to controller .
I tried to encode this params but it does not work like this:
params = encodeURI(params);
but it still get 'R' instead of all value.
Please help me, thanks

In javascript, you should use encodeURIComponent to encode parameter in url :
var params = '?DepartmentId=' + DepartmentId + '&DepartmentName=' + encodeURIComponent(DepartmentName);

Related

Passing a List<Map> through location.href

My problem is, I want to pass a parameter which is a List to my location.href
function addNewDriver()
{
var pEntityType = <%=c.WIDOC_ENTITY_DRIVER%>;
var pListEntities = <%=lListDrivers%>;
location.href= "<%= c.url %>do/user/groupItemForm.step1.jsp?idClient=" + <%=pIdClient%> + "&listEntities:" + pListEntities +
"&idGroup=" + <%=pIdGroup%> + "&entityType=" + pEntityType + "&<%= lBackButtonUrl %>";
}
This is an example in Chrome console.
var pListEntities = [{dniPerson=4444444S, surname2Person=XXX, passportPerson=null, namePerson=XXX, idGroupItem=1, idPk=1111, surname1Person=XXXX};
This is how I think "they" wanted it.
var pListEntities = [{dniPerson:4444444S, surname2Person:XXX, passportPerson:null, namePerson:XXX, idGroupItem:1, idPk:1111, surname1Person:XXXX};
"lListDrivers" is already filled with values of each driver, like dni, name, surname etc.
The problem comes because, to pass this list, values inside can't have an equal, they need a ":", but I don't know any way to change it.
Convert your List<Map> to JSON. you can use Jackson ObjectMapper to convert it and it will change your pListEntities to a JSON object.
Here is one example.

replace segment of a URL with regex

I have a button that takes a value from a checkbox and inserts it dynamically into a URL parameter. the URL looks like this:
example.com/search?q=searchterm&site=site1&page=1
&site=site1 is what is being updated dynamically by the value of a checkbox. My code for that looked like below at first:
$("#apply-filter").click(function() {
var filterSite;
var filterSelection;
var filterUrl;
filterSite = "http://" + location.host + location.pathname + location.search;
filterSelection = $('.search-filter-dialog input[type="checkbox"]:checked').val();
filterUrl = '&site=' + filterSelection + '&page=1';
console.log(filterUrl + " - " + filterSite);
window.location.replace(filterUrl);
});
The problem with the first approach is when you click the button multiple times, it just adds the new parameters to the URL, so it ends up looking like:
example.com/search?q=searchterm&site=site1&page=1&site=site2&page=1&site=site3&page=1
When I only need to change &site=site3&page=1 - I tried using a regex to select that part of the URL and replace it with the new one. My attempt at that is below:
$("#apply-filter").click(function() {
var filterSite;
var filterSelection;
var filterUrl;
filterSite = "http://" + location.host + location.pathname + location.search;
filterSelection = $('.search-filter-dialog input[type="checkbox"]:checked').val();
filterUrl = filterSite + '&site=' + filterSelection + '&page=1';
var url = filterUrl.match(/&([^ ]*)/)[1];
console.log(filterUrl + " - " + filterSite);
window.location.replace(url, filterUrl);
});
What this block does is remove the search query and just returns
example.com/site=site1&page=1 which gives a 404.
I need to somehow update a segment of a URL, and not the entire thing. I believe I need to do some sort of regex to target it and change it. What are the next steps? How can I update a certain section of a URL?
EDIT: This is where it stands now:
// apply filter, go to page
$("#apply-filter").click(function() {
var filterSite;
var filterSelection;
var filterUrl;
filterSite = "http://" + location.host + location.pathname + location.search;
filterSelection = $('.search-filter-dialog input[type="checkbox"]:checked').val();
filterUrl = filterSite + '&site=' + filterSelection + '&page=1';
var url = filterUrl.match(/&([^ ]*)/)[1];
// console.log(filterUrl + " - " + filterSite);
if (window.location.href.indexOf("&site=") > -1) {
filterSite.replace(/&site=\d+/,'&site=' + filterSelection);
window.location.replace(filterSite);
console.log(filterSite);
} else {
window.location.replace(filterUrl);
}
});
but the .replace() method doesn't seem to be working.
Correct me if I got it wrong:
You have something like this: example.com/search?q=searchterm&site=site1&page=1 and you need to update ONLY THIS PART: &site=site1.
One way:
filterSite.replace(/&site=site\d+/,'&site=site' + filterSelection);
This works only if the updatable part of the url is ALWAYS going to be of the form &site=site<number>, ie: filterSelection is always a number
anyhow, let me know
REGARDING YOUR EDIT:
Assuming what you mean by The .replace() method won't change the parameter, is that the URL won't change, you are right: when you do this:
filterSite.replace(/&site=\d+/,'&site=' + filterSelection);
what you are modifying is the variable filterSite, the page won't automatically reload to the new url, which I think is what you intend after seeing this other line:
window.location.replace(filterSite);
replace it with:
window.open(filterSite);
to make the page go to the new url
More about window.open and its arguments
One last thing, I noticed you are using /&site=\d+/,'&site=' + filterSelection as args for replace which will not match example.com/search?q=searchterm&site=site1&page=1. So, unless you changed the structure of the url, you might want to look on that too.
let me know
URL Constructor
EXAMPLE: 1
;
var uri = new URL( "http://example.com/search?q=searchterm&site=site1&page=1" );
uri.searchParams.has("q");
>> true
uri.searchParams.set( "site", "site2" );
uri.searchParams.set( "page", "2" );
uri.href;
>> "http://example.com/search?q=searchterm&site=site2&page=2"
;
Browser Suport: ( ? )[ Chrome 49, Firefox 44, Opera 36, IE12 ]
I suggest you to use A POST request method and Jquery has already a method to construct that params to send based in the form
https://api.jquery.com/serialize/

Cannot pass URL from JavaScript to PHP in Laravel

I'm trying to get three values from a front-end form in JavaScript and then passing those values to the Laravel controller.
I can get two values (Key and Id) but I cannot get the third value because it is a URL. If I comment the URL variable then I'm getting the rest of the two values (Key and Id) but if I'm passing the URL along other two values then I'm not getting anything in my controller.
I have tried these two JavaScript methods encodeURIComponent() and encodeURI() to encode the https URL given but still no luck.
This is my code in JavaScript:
let get_url = $("#get-url").val();
let get_key = $("#get-key").val();
let get_id = $("#get-area").data('getId');
toastr.remove();
if (get_key) {
var req = new Image();
req.src = api_url + 'senddata/' + get_id + '/' + get_key + '/' + get_url;
console.log(req.src);
toastr.success(MEASURE.UTILITIES.translate("success_request_successfully_sent"));
$("#btn-send-data-request").hide();
$("#block_get_url").hide();
$("#block_get_key").hide();
$("#csv_get_request_message").show();
} else {
toastr.error(MEASURE.UTILITIES.translate("error_enter_valid_data"));
}
Your get_url contains a url, which i believe has https:// or http://
This is a problem when you are adding to the API URL you are constructing as adding more / changes the ROUTE.
For example your API URL expects req.src = api_url + 'senddata/' + get_id + '/' + get_key + '/' + get_url;
which is something like http://www.laravel.com/api/senddata/123/123/urlhere but you are making is something like http://www.laravel.com/api/senddata/123/123/http://urlhere which means you are altering the ROUTE.
To solve that you can URL Encode the URL before passing it.
Change your code to this should work : let get_url = encodeURIComponent($("#get-url").val());
Reference: https://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

loading saving and showing json results in javascript

I have a php file called rows2.php that shows results like so after entering new fields in a database. It is simply showing the new id of the field :-
{'new_id':'92'}
I want to load this with javascript and add the new_id to existing list with : either side of the number and display it but I seem to be struggling? Many thanks.
The javascript to load the page and get the result is :
$.getJSON("rows2.php", function(result) {
var new_id=console.log(result[0].new_id);
document.getElementById('vehicle_list').value = '' + document.getElementById('vehicle_list').value + 'new_id' + ':';
})
You should use
document.getElementById('vehicle_list').innerHTML = ''+document.getElementById('vehicle_list').innerHTML+'new_id'+':';
instead of
document.getElementById('vehicle_list').value = ''+document.getElementById('vehicle_list').value+'new_id'+':';
.value is used only in case of input elements otherwise you must use .innerHTML
Don't put the variable name (new_id) in quotes.
document.getElementById('vehicle_list').value = '' + document.getElementById('vehicle_list').value + new_id + ':';

Using URL encoded parameters in Angular

I am passing an encoded URL to an AngularJS app, what I want to do is decode the value and then take the parameters to use them to fill some html elements. So I have the following encoded URL:
http://www.otherurl.com/ROOT/?Transaction=%257b%2522TransactionID%2522%253a%2522-1%2522%252c%2522CashierID%2522%253a506069%252c%2522ErrorDescription%2522%253a%2522%2522%252c%2522ErrorCode%2522%253a%2522%2522%252c%2522status%2522%253a500130%252c%2522statusName%2522%253a%2522Approved%2522%252c%2522TraceID%2522%253a%2522%2522%252c%2522externalID%2522%253a%2522%2522%252c%2522Amount%2522%253a45.00%252c%2522CurrencyCode%2522%253a%2522USD%2522%252c%2522RejectReason%2522%253a%2522%2522%252c%2522Notes%2522%253a%2522%2522%252c%2522CSID%2522%253a-1%252c%2522ProcessorName%2522%253a%2522%2522%252c%2522Descriptor%2522%253a%2522%2522%257d#/return
I have the following code on js:
var uri_dec = decodeURIComponent(decodeURIComponent($location));
var nuevo = uri_dec.search()['Transaction'];
console.log('return controller > dec: ' + uri_dec);
console.log('return controller > nuevo: ' + nuevo);
$scope.transactionID = nuevo.search()['CashierID'];
console.log('return controller > transactionID: ' + $scope.transactionID);
Issue here is that Im not being able to use "nuevo" as it says undefined. Would you please help me solving this?

Categories

Resources