How to get "&" in urlParams.get in Javascrpt - javascript

I'm trying to get the parameter used in URL with java script.
example URL:
`http://localhost:8002/all-products?categories=Events%20&%20Festivals`
I use this code to get the value in categories.
const urlParams = new URLSearchParams(queryString);
const category_name = urlParams.get("categories")`
I only get Events as an output i need to get Events & Festivals but i cant seems to get it

you can add some other character instead of &
for eg. '-'
`http://localhost:8002/all-products?categories=Events-Festivals`
OR
you can encrypt the category value and pass it in URL

Related

There is way to save only part of the string object into async-storage?

There is way to save only part of the string object into async-storage ?
for example if inside the "result.userPrincipalName" it save " bob23#hotmail.com "
so i want it will be save only the "bob23" , so what is the way to do it ?
await AsyncStorage.setItem(
'AZURE-USERNAME',
JSON.stringify(
result.userPrincipalName.substring(0, data.indexOf('#'))
)
);
You condo something like this.
You can remove part of the string after a certain character.
As I can see in the documentation. It stores as a key-value pair.
So I made changes
let data = JSON.stringify(result.userPrincipalName);
//here substring will remove characters after and including `#`
data = data.substring(0, data.indexOf('#'));
await AsyncStorage.setItem('AZURE-USERNAME', data);
I believe that you need to handle your desirable data on top level (add new input to user / new field in db with desired data etc. I don't know whereby you get your userPrincipalName).
But, if it's not possible, you can follow something like this:
const name = result.userPrincipalName.split('#')[0];
if (!name) throw new Error('Invalid email');
await AsyncStorage.setItem('AZURE-USERNAME', name);
Simply you can use split function in Javascript. More information
//Split the mame using split function
const splitName = result.userPrincipalName.split("#");
//Get the split value and save it
//After you splitting [0] mean first character set
const name = splitName[0];
//Save it to AsyncStorage
await AsyncStorage.setItem("AZURE-USERNAME", JSON.stringify(name));

http_build_query and URLSearchParams

I have a from with inputs with array name like foo[] so when submitting the form my url become foo[]=lorem&foo[]=ipsum but when using php http_build_query to regenerate this sort of query it adds key to the array like this foo[0]=lorem&foo[1]=ipsum.
My issue is that on the front I'm using URLSearchParams.getAll('foo[]') to get the value, but it works only without the array keys added by http_build_query.
So is there a way to use URLSearchParams with this format ?
Based on #Plamen Nikolov link I fixed my issue by changing my Javascript to handle the inputs that are arrays with or without index keys (added by PHP with http_build_query) like this :
// Before
let urlParams = new URLSearchParams(window.location.search);
// After
let urlQuery = decodeURI(window.location.search).replace(/\[\d+\]/g, "[]");
let urlParams = new URLSearchParams(urlQuery);

use express.js to get the query string of a url

I'm looking for a way to get the query string part of a url using node.js or one of it's extension module.
I've tried using url and express but they give me an array of parameters.
I want to original string.
Any ideas ?
example; for:
http://www.mydom.com?a=337&b=33
give me
a=337&b=33
(with or without the ?)
Use url.parse. By default it will return an object whose query property is the query string. (You can pass true as the second argument if you want query to be an object instead, but since you want a string the default is what you want.)
var url = require('url');
var urlToParse = 'http://www.mydom.com/?a=337&b=33';
var urlObj = url.parse(urlToParse);
console.log(urlObj.query);
// => a=337&b=33
How about using the built-in url.parse method?

JSON data being received in an incomplete format in php?

I'm building a super simple website application that calls an API to retrieve backlink data for any website inputted into said application by the user. The data the API sends includes strings (e.g. http://www.domain.com/?feed=rss) and numbers and is in JSON format. I parse the response as follows:
mozResponse = JSON.parse(response);
I then iterate through this data, pushing only the data I want into 2 new arrays (arry, arry1), declared as follows:
arry = [];
arry1 = [];
Pushing as follows:
arry.push({id:i, url:mozResponse[i].uu, pa:Math.round(mozResponse[i].upa), da:Math.round(mozResponse[i].pda), anchor:mozResponse[i].lt});
I then Stringify these two arrays as follows:
var cautionArrayString = JSON.stringify(arry);
var dangerArrayString = JSON.stringify(arry1);
I'm using a JavaScript XMLHTTPRequest to POST this data to a php file as follows:
var queryString = "email=" + Email + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;
xhr1.onreadystatechange=Response1;
xhr1.open("post","http://example.com/emails.php",true);
xhr1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr1.send(queryString);
The php file then reads:
$caution = $_POST['caution'];
$danger = $_POST['danger'];
I'm aware at this point I should decode the JSON again upon originally doing so I ended up with a broken array. Here's what the console.log reads AFTER posting the data to the php file, with the php file using:
echo($_POST['caution']);
echo ($_POST['danger']);
Console.log shows:
[{"id":3,"url":"example.ca/","pa":26,"da":12,"anchor”:”Example”},
{"id":4,"url":"example.ca/","pa":26,"da":12,"anchor":"thank you gifts"},
{"id":5,"url":"example.ca/","pa":26,"da":12,"anchor":"flowers"},
{"id":6,"url":"example.ca/","pa":26,"da":12,"anchor":"thank you"},
{"id":7,"url":"example.ca/","pa":26,"da":12,"anchor":"Arrive in Style"},
{"id":8,"url":"example.ca/","pa":26,"da":12,"anchor":"dignity"},
{"id":9,"url":"example.ca/","pa":26,"da":12,"anchor":"Beautiful in Blue"},
{"id":10,"url":"example.ca/","pa":26,"da":12,"anchor":"Blooming Garden Basket"},
{"id":11,"url":"example.ca/","pa":26,"da":12,"anchor":"Country Basket Blooms"},
{"id":12,"url":"example.ca/","pa":26,"da":12,"anchor":"Heart’s Delight"},
{"id":13,"url":"example.ca/","pa":26,"da":12,"anchor":"Make a Wish"},
{"id":14,"url":"example.ca/","pa":26,"da":12,"anchor":"Moondance"},
{"id":15,"url":"example.ca/","pa":26,"da":12,"anchor":"Queen’s Court"},
{"id":16,"url":"example.ca/","pa":26,"da":12,"anchor":"Sweet as Sugar"},
{"id":17,"url":"example.ca/","pa":26,"da":12,"anchor":"flower colors"},
{"id":18,"url":"example.ca/","pa":26,"da":12,"anchor":"Always Yours"},
{"id":19,"url":"example.ca/","pa":26,"da":12,"anchor":"Sunrise, Sunset"},
{"id":20,"url":"example.ca/","pa":26,"da":12,"anchor":"Uniquely Chic"},
{"id":21,"url":"example.com/best/index.php?page=1998","pa":25,"da":31,"anchor":"example.ca/"},
{"id":22,"url":"example.com/best/index.php?page=1994","pa":25,"da":31,"anchor":"example.ca/"},
{"id":23,"url":"example.ca/","pa":25,"da":16,"anchor”:”example”},
{"id":28,"url":"example.ca/article/156-best-cms-for-small-business","pa":22,"da":39,"anchor":"example.ca/"},
{"id":30,"url":"example.ca/blog.html","pa":21,"da":15,"anchor":"example.ca/"},
{"id":31,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor”:”Example”},
{"id":32,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Arrive in Style"},
{"id":33,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Beautiful in Blue"},
{"id":34,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Blooming Garden Basket"},
{"id":35,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Country Basket Blooms"},
{"id":36,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Heart’s Delight"},
{"id":37,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Make a Wish"},
{"id":38,"url":"example.ca/gifts.html","pa":19,"da":11,"anchor”:”example- <span class=\"catlist\"> Flowers</span>"},
{"id":39,"url":"example.ca/category/flowers/","pa":19,"da":16,"anchor”:”Example”},
{"id":40,"url":"example.ca/category/floral-fauna/","pa":19,"da":16,"anchor”:”Example”},
{"id":41,"url":"nunavut.findstuffhere.ca/nunavut/?feed=rss2[]
Where you can see AT THE END that the 1st array is incomplete and the 2nd is empty (as it should be!). So my question here is, what's causing this and how can I fix it?
Things to Note
I use 3 URL's as inputs to test. The data is complete with 1 URL, but 2 others (the example above included) return this incomplete JSON, seemingly because of a query string being a part of a URL returned from the API?
I console.log(mozResponse) and the output is as expected
I console.log(arry) and console.log(arry1) AFTER iterating through mozResponse and pushing data from mozResponse to arry/arry1 and the output is a complete array
I console.log(arry) and console.log(arry1) AFTER applying JSON.stringify but BEFORE posting to php and the output is complete
Things I've explored
I originally thought this was the result of one of the URL's that the
API returns including a query string (e.g. ?feed=rss2 where it
breaks) however prior to this break point there are query strings
being handled fine
Doesn't seem to be a JSON error as I used
json_last_error(); and it returned 0.
Also doesn't seem to be a a
JSON/POST char limit thing because it returns broken JSON when I
input a different URL (the output from the $_POST for this URL also
breaks following a query string, not sure if this is coincidence)
Suhosin isn't present
Created a phpinfo page to check max_vars but the limit is large
Any help is greatly appreciated!
*different quotes are from copy/pasting!
You probably just need to encode your values for use in a query string:
var cautionArrayString = encodeURIComponent(JSON.stringify(arry));
var dangerArrayString = encodeURIComponent(JSON.stringify(arry1));
var queryString = "email=" + encodeURIComponent(Email) + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;
Converting it to json does not automatically encode it correctly for use in a url so characters in your values could break the query string.
Assuming the different quotes are caused by copy-pasting...

TaffyDB: Select problems

I'm new to TaffyDB and haven't done a lot of javascript programming so I'm hoping that the problem I'm having is something simple. I'm trying to update a listbox with the options stored in the TaffyDB according to the selected client. When I do my select however, it is returning all the rows.
Below is the code I am using to update the listbox, along with the selectString used to do the query, and what's in the TaffyDB.
Anyone have any ideas why I am getting back all rows when I specify clientID = 1788?
I tried the select string with and without quotes around the column identifier.
// load existing user client projects if we have any
var lbProjects = document.getElementById('lbProjects');
lbProjects.options.length = 0;
var selectString = '{clientID:"' + clientID + '"}';
alert(selectString);
userProjects(selectString).each(
function (r) {
var option = new Option();
option.value = r.projectID;
option.text = r.projectName;
lbProjects.add(option, null);
});
What's in selectString:
{clientID:"1788"}
What's in the DB:
[{"clientID":"1788","projectID":"19"},
{"clientID":"1789","projectID":"24"},
{"clientID":"1790","projectID":"23"}]
Thanks for any help.
Aaron L. Bratcher
The problem was trying to use the selectString variable.
The line
userProjects(selectString).each(
now reads
userProjects({clientID: clientIDValue}).each(
I was supposed to be passing in an object array, not a string. {} in javascript creates an array of objects.

Categories

Resources