How to add double Quotes in Query params - javascript

I have a url in API REST firebase and add a query params with double quotes but when i send services this quotes transform to %22, i need don't transform
i need:
https://{BASEURL}/students/admin_stundents/.json?orderBy="index"22&limitToLast=25&auth=
tranform to:
https://{BASEURL}/students/admin_stundents/.json?orderBy=%22index%22&limitToLast=25&auth=
resove this problem

Related

How can i remove double quotes from JSON data in react native

Here is my code
I am not able to remove double quotes from json data and store in Async storage

$location strips slashes from search parameters

I need a url with a query string like this:
/api/search/?path=parent/parent/parent/child
So I use $location like this:
$location.search('path', 'parent/parent/parent/child')
But it ends up with the delimiters stripped like:
/api/search/?path=parentparentparentchild
Is there a way to get the unescaped slashes without decoding them? I want to avoid:
?path=parent%2Fparent%2Fparent%2Fchild

Twitter Search by Hastag missing parameters?

I am using the below call to Twitter API
https://api.twitter.com/1.1/search/tweets.json?q=#iosgames
I get the response:
{"errors":[{"code":25,"message":"Query parameters are missing."}]}
According the this the only parameter that is required is q for the query string.
The issue isn't with my OAuth as it fine with the status/user calls.
What am I missing?
The pound sign (or hash sign, if you prefer) is one of those which must be encoded in order to be sent in a URL. # is represented by %23, so your request should be for:
https://api.twitter.com/1.1/search/tweets.json?q=%23iosgames
You need to url-encode your hashtag:
https://api.twitter.com/1.1/search/tweets.json?q=%23iosgames
Reference

What is the correct way to embed (string) data in element attibute?

I would like to send data from the server to the client, where javascript code can access the data. This is basically a string message, what I would like to embed in my custom attribute like this:
<div my-message="here is my custom data">
After short testing I recognized the message itself can contain special char like " so the html will be incorrect after embedding.
What is the correct way to (encode?) the string data? (server side is ASP MVC)
Is there any javascript support to decode the string? Obviously say base64 can do it, but it sounds a bit weird, and also I would not like reinvent the wheel here.
For server side:
Razor
<div data-my-message="#("your string with \" goes here")">
Or if you hold the string in the Model
<div data-my-message="#Model.Message">
ASPX
<div data-my-message="<%: "your string with \" goes here" %>">
Razor's #() or ASPX's <%: %> will encode it correctly.
BTW If you want to embed your string directly in the view you can escape the " with \"
For client side:
You can read it easily with jQuery
$(selector).data("my-message")
or plain javascript
document.querySelector(selector).dataset.myMessage
Also you should prefix your custom attributes with data- as #Erik Philips says in the comments
You can use the functions encodeURI and encodeURIComponent for encoding. And for decoding the functions decodeURI and decodeURIComponents.
You can look them up for example here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
If you want to send arbitary data (i.e. arrays, objects, etc.) then I use the following:
use a data-foo attribute on the tag
json-encode the data
Use single quotes around the attribute value (JSON uses double quotes for strings)
Replace any single-quotes in the json-encoded string with the corresponding thml entity (&#8217)
Something like (I did it in PHP so the .net syntact might be slightly wrong
<% Dim Serializer As New System.Web.Script.Serialization.JavaScriptSerializer %>
<div data-foo='<%=Serializer.Serialize(MyData).Replace("'", "&#8217") %>'>Some content</div>
Where the data to use is in the variable MyData
Has the advantage that when you get out the data you get an object back automatically (at least, with jQuery but I would imagine with plain js too - if not then it's easy to recreate the object)
Update
Following the comment about double quotes, if your data is just a string then you should replace double quotes buy their html entity too. Or ensure you json-deode. It's not automatically recognised as json data and decoded if it's just a string. PHP (where I have used this more) encopdes double quotes in the string as \". I have a memnory that in vb.net you need to change Serializer.Serialize(MyData).Replace("'", "&#8217") to Serializer.Serialize(MyData).Replace("\", "\\").Replace("'", "&#8217") becuase of how it handles quotes. If the data is an object then it is automatically json-decoded when you get it out and alls is OK. if the data is just a string then don't need to json-encode maybe - just change at least one of the type of quotes (either single or double quotes) to html entities and use that quote type around the attribute.
As long as you get the quoting right it handles html in the data fine: see http://jsfiddle.net/ctueo7sr/ for a simple string example and http://jsfiddle.net/aLaLnhp2/1/ for an object example

Escape '/' in angularjs variables

I would like to know how I can escape / in angularjs so that I can send it as a path variable to call a restful service. As of now I am getting 404 whenever I try to send a url with path variable values having /; even if I encode it doesn't work.
For example
http://monish.home.com/payment/9a2c1ae67d4ff85e561679fcff/credit/%252B8VMWj/YBC%252FNj3l/fetch/options
Is there a way where I can encode and escape the /?
Yes, its called encodeURIComponent
var a = 'http://monish.home.com/payment/9a2c1ae67d4ff85e561679fcff/credit/%252B8VMWj/YBC%252FNj3l/fetch/options'
encodeURIComponent(a); // result: "http%3A%2F%2Fmonish.home.com%2Fpayment%2F9a2c1ae67d4ff85e561679fcff%2Fcredit%2F%25252B8VMWj%2FYBC%25252FNj3l%2Ffetch%2Foptions"

Categories