How to email a parsed JSON using a script [closed] - javascript

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)

Related

Best way to parse URL in ES6 [closed]

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 4 years ago.
Improve this question
I've seen some legendary questions about the topic like that: how to parse a url .
But years passed and things changed. The answers from questions that I can find is out of date.
I don't want to parse URL via regexp or some hack like creating HTML node as a parse helper. I want some flexible method that returns an object with all required data from the URL.
I believe that there are some new built-in methods to do it or new revolutionary amaizing and simple ES6 libraries for that purpose.
Can you please advice something like that?
I think you are looking for web api's URL() constructor like this:
const myTestURLString = "https://www.youtube.com:8080/watch?v=YaXXXXkt0Y&id=123";
const myURLObj = new URL(myTestURLString );
console.log(myURLObj.protocol);
console.log(myURLObj.host);
console.log(myURLObj.hostname);
console.log(myURLObj.pathname);
console.log(myURLObj.search);
console.log(myURLObj.searchParams.get('v'));
console.log(myURLObj.searchParams.get('id'));
ES6 is part of the language specification, not any particular framework for JavaScript. Therefore, you're not going to find things like URL.parse() in the language.
The APIs you're looking for are part of the host for your application, such as the browser or Node.js. In browser, there is a URL interface: https://developer.mozilla.org/en-US/docs/Web/API/URL

Logged user is AnonymousUser on specific view [closed]

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 problem that on specific view request.user returns AnonymousUser.
This is caused by a javascript library which I use to collect payments. That javascript library makes a cookie which makes django see a logged-in user as AnonymousUser.
If I delete that cookie, django sees the user as logged-in but after a couple of refreshes, I get a new cookie which makes again the logged-in user an AnonymousUser.
And I have this issue only in one specific page where that library is inserted in the page.
Any ideas what is wrong?
The javascript in question sets a cookie by the name mistertango[collect][mt_fp].
When cookies was defined (RFC 6265, I guess) it seems they didn't really specify what characters you're allowed to use in a cookie name, other than basically «text».
This causes some problems with parsing cookie names. Django relies on Python's http.cookies for this, and it seems http.cookies doesn't allow brackets in cookie names. http.cookie failes to parse cookie pairs with brackets in it, and doesn't parse pairs after that which means it doesn't see the sessionid cookie it uses for authentication.
I'm not able to tell if Django/http.cookie should or shouldn't support this.
PHP does however seem to support it (even if it's broken), while Ruby on Rails does not.
The easy solution is to use only alphanumeric characters in cookie names.
For your case, the best solution is to get the javascript author to change their cookie name. If that's not possible, or in the mean time, you could host the javascript yourself and change the cookie name in your copy. (This may not work if the cookie is used for something outside of this javascript snippet, but I don't really understand Javascript and does not see what it is used for.)

Ways to store dynamic data produced by jquery [closed]

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 7 years ago.
Improve this question
I am learning web development. I have come across a part in jquery where i can dynamically produce html elements like div table etc. But i wanted to know how to store these dynamically produced data. What are all the ways to store these jquery dynamic data?? And if i want to store it in mysql how to do?? It will be helpful if you have any reference links or code. Pls help me with this
The dynamic data often comes from a JSON string. JSON represents an object (or a set of objects) that can be used directly in Javascript.
Using Jquery, you can get JSON with this utility method : getJSON( url [, data ] [, success ] )
So you have to provide an URL responding with JSON, to be used as the url parameter in getJSON function.
It can be either a static file (ie http://domain:port/data/myData.json) or a dynamic content generated by a server side process in the language of your choice (PHP, Java, JS with NodeJS...), ie http://domain:port/myData.php?filter1=value1.
Note : JSON is the most standard format for transferring objects to Javascript, but it could any format (CSV, XML, ...)
In the callback method of getJSON, you will be provided a plain Javascript object that will contains everything that was in your JSON file, and you could use it to produce whatever you want (html elements like div table etc).
If you need to request a SQL backend, you will have to use a server side process to do the SQL request and map it back to a JSON object.

PHP Get Values from a URL using GET[] [closed]

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()

Getting HTML or JSON response from ajax call, Which is good? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a functionality in my project where I have implemented the search functionality.
On submitting the form via ajax, I need to show all the results in a division.
There are two ways I can do this.
Getting the JSON data as ajax response and bind it to the HTML elements.
I can also get the completely formatted HTML response from the ajax call and can directly bind it to the result div on the search page.
So which way is suggestible ?
To make a service (server-side script) the most re-usable or even make it into an API - the suggested way is to return JSON data (converted from data models) to the front end, where using JavaScript you can populate the data to the HTML.
As for the HTML - you can certainly make the server return the response as HTML (setting the correct mime & content type in headers) but this gives the server control over the UI layer and the separation between the interface and the server/db is not balanced properly...
Either option is fine, depending on your how much html there is and how much server-side processing you need to do on the HTML. If it is just a div and a value that needs to be inserted, then I say just go with JSON. The JSON approach will be more lightweight (consumes less bandwidth and keeps the role of the server as an API that is transferable to non web-page requests).
If you need to do a lot of server-side processing and assembling and what you are returning is really a sub-page, then you might consider html from the server. In this case have a partial html file that you read and send (inserting data where relevant) rather than building the html from strings on the fly. If you have a partial file, then you can edit and check it with standard html editors and you can see the html layout easily and it keeps the UI aspects separate from the business logic.
I'd send it as JSON and build the HTML client side for a few reasons:
The JSON payload would be lighter and therefore faster to send over the wire
The API becomes more reusable (if you ever wanted to hook up additional clients that render differently etc.)
If you build the HTML on the client side then it's probably easier to take advantage of templating libraries (e.g. JQuery Templates) or even better, directly binding the data to the UI (such as Knockout)
I always do the JSON response. To me it looks like a much more consistent and flexible way since you can return more data than only presentation. If you still want to return HTML you can also do it through a JSON response:
{
error: false
html: "<div>Done!</div>"
}
I'd send it as a JSON response, too.
As suggested by emiolioicai in his code, with JSON you can easily handle errors. For example:
{
error: true
error-message: wrong parameters
}
If you define your HTML client side, in the future you will be able to use the same AJAX request and customize HTML differently in another part of your website.

Categories