Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm having one big trouble.
When I send post request with angular, and do JSON.Stringify it returns (unable to decode value). I don't know how to solve it
Use angular.toJson() method instead. Because some internal notation are used by angularjs.
https://docs.angularjs.org/api/ng/function/angular.toJson
Serializes input into a JSON-formatted string. Properties with leading
$$ characters will be stripped since angular uses this notation
internally.
This is not an error of malformed JSON , you are getting this error cause you are using POST along with the following Request Header : ("Content-Type","application/x-www-form-urlencoded"). In that case you will have to use encodeURIComponent() along with JSON.stringify() on your JSON Object in order not to get the above error.
If your Request Header is ("Content-Type","application/json") you do not need to use encodeURIComponent() , simply using JSON.stringify() is sufficient.
Note : You could also use GET and wouldn't get the error , but GET has length limitations in your message and is usually avoided when sending JSON)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a script that gets and parses a JSON, I want to email the JSON values directly from the script when the page loads. I already have everything set up but do not know the best approach about doing this. I usually use forms to send information but this is a little different.
I am not looking for someone to hold my hand and show me how, I just want to know the different options and I can figure it out myself.
I don't think there is any native javascript functionality for sending emails. I would use a service like https://www.emailjs.com/ or the Gmail javascript API if it was totally necessary to do this from javascript.
If you want to automatically send an email using plain javascript from the browser, you can't. You'll have to setup node & use something like nodemailer:
https://nodemailer.com/
If this isn't the case, you can use window.open and pass the email data in this way. It will open the default email client on your computer & pre fill an email with the parsed information. Like so:
window.open('mailto:your#email.com?subject=your_subj&body='+YOUR_JSON_HERE);
Make sure your passing json and not a javascript object. If you're passing a js object it will return [object object]. If this is the case you'll need to stringify the js object, like so:
JSON.stringify(JS_OBJ)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a rest call that sends addresses to the back-end. When an address contains a / e.g. c/o (corner of) I get a 400(bad request... i've tried to encodeURIComponent(query) but even though the address is encoded it still gives me the same error. i'm planning on replacing each occurance of / with $ in the rest call and then replacing the $ with a / again on the back-end. Is this the only way to go about this or is there a propper way of doing this?
The forward slash or / is a special char in URI encodings, it is used to separate arguments for the path to the file we want to access. So it obviously can not be used as we please. The encodeURIComponent function from Jquery will encode this character but it will be interpreted by your server as a keyword in most cases.
Your solutions are:
Either send using POST, which is the cleanest way to send text without having to care about its encoding.
Or replace the / with its html enity code which is: /, that way, you can send it via GET and it will still be understood by HTML as a / but no longer as a special character.
Hope it helps
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I'm using plain javascript (no jQuery allowed) and I'm having trouble making Ajax call the old way, one of my variables content has single quote in it:
To escape it I'm doing :
url = url.replace(/'/g, "\\'");
I console log the output, the single quote is replaced by %27 Then I'm calling it:
xhr.open("GET", url, false);
But I still get a 500 internal server error
PS: No error when I remove the single quote from the content.
When you build the url, use the escape() function to encode the value of the variable you are inserting.
eg. instead of
var url = "http://host.dom.com/path/file.php?var=" + myVar
use:
var url = "http://host.dom.com/path/file.php?var=" + escape(myVar)
\ is not how you escape URL characters.
Personally, I would advise against GET for AJAX requests, unless you're getting static or template-based content (even then...)
You would be much better off using POST, and you can POST any string you like without worrying about encoding.
However, if you want to use GET, or POST with a urlencoded format (so that PHP's $_POST will work, for example), then you need to encode correctly, which is done with escape()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to develop products filters for an online store I am working on. An example of what I mean is http://www.riverisland.com/men/just-arrived. I have managed to get a JavaScript to populate the URL when the sizes are clicked on but failed to get them remove value from URL when unchecked.
My main question here is this. Assuming I have my URL as:
http://127.0.0.1/shop/dresses/?s=1&s=2&s=3
How do I get my PHP to extract the values from the URL?
How do I format a SQL query to search the values gotten from the URL using any sample query?
An easier solution is this.
Format your URL like http://127.0.0.1/shop/dresses/?s=1,2,3 as suggested by #Andrey.Popov. Then do the below.
if(isset($_GET['s']) && !empty($_GET['s']))
{
$e = sanitizeFunction($_GET['s']);
$d=explode(',',$e);
}
$d now has all your $_GET['s'] values.
That's the easier way I have figured out and it works!
In order to benefit from $_GET and other superglobals you have to follow the rules explained at Variables From External Sources. Since you've chosen to have several parameters with the same name and they do not contain properly paired square brackets you're basically on your own. The manual steps you must reproduce include:
Extract the raw query string from $_SERVER['QUERY_STRING'], e.g.:
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING');
Parse out the string. As far as I know, there aren't built-in functions that do exactly this so I'd either google for a good third-party library or write a simple parser with regular expressions or good old explode().
Decode the URL-encoded values with urldecode()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to consume data from an API using REST call .
I've succeeded to get data from the API :
You can check demo on jsfiddle : http://jsfiddle.net/zTXyq/33/
Now , I am trying to perform the work to get different resources from this API
I am reading these two articles :
- http://www.jquery4u.com/demos/ajax/
- http://welcome.totheinter.net/tutorials/model-view-controller-in-jquery/
I would like to know why do we use JSON.stringify() if we can show data in HTML pages .
Why do we use so-called 'class'/'model' to parse data using json ?
Regards ,
JSON is a data structure, which means it serves to transport variables, arrays and stuff.
Sending variables in html code would be silly - that is why we use JSON.
JSON.stringify() is an utility function, which is most commonly used for debugging, when you want to print some variable to console, but there are some other uses, too.
console.log( JSON.stringify(myObject) );
What the function does, is that it takes an object, array or about anything else, and makes a readable string of it, while simply using something like this ↓↓ would give you only [Object].
console.log( myObject );
If all you really want is to send some HTML to be displayed, then you don't need to bother with JSON at all, and just use jQuery.load() or something similar.
I hope you will agreee that in javascript code JSON objects are nice and easy to use, for example
var jsonObj { "p1": "1", "p2": "2"};
if ( jsonObj.p1 == '1' ) { // do something }
But if we want to pass that object around to non javascript code, for example to a PHP script, we cannot do it.
So the stringify comes into play, we stringify our JSON object i.e. convert the whole object to a string, now we can pass this around to anything that has the ability to convert it back to something useful. So PHP has the json_decode() function to convert a stringified json object/array to a PHP object/array so it becomes useful again.
Now when we want to pass data back from PHP to javascript we convert a PHP object/array to a json string using json_encode() and we can now pass an object/array back to a javascript as a string for it to use JSON.parse() on so it can convert it back to a usable object/array.
So in summary, stringify allows us to convert language specific formats for objects/arrays into a universally available format i.e. a string. Therefore any language that provides a method of converting this stringified format into its own language specific formats for objects/arrays can use this data and of course return it again via the stringified format.