getJSON explanation - javascript

$.getJSON(
main_url + "tasks/",
{ "task":8, "last":lastMsgID }
I don't really know how this works, but I need a URL or something that can get me messages from a shoutbox and this is what the function uses by default. Does it use a URL and if so how do I plug in the "task" and "last" to the URL? Or is it some other method?

I'm also not sure i fully understand what you want, but a quick explanation of what's going on there. If you check out the api for getJson you'll see that the first parameter is the url to query from and the 2nd is optional data to send to the server. So in your case, the url would be main_url + "tasks/" the data sent to the server is { "task":8, "last":lastMsgID }
If your main_url is something like www.domain.com then your whole request will look like this:
http://www.domain.com/tasks?task=8&last=xxx
where xxx is the lastMsgID

Related

How to replace first # with ? in querystring and then forward to server, with Javascript?

my browser is receiving a # in a redirect url instead of a ? ie:
localhost/endpoint#code=12345
However everything after # gets removed before the server endpoint is called. Theerfore I need to replace the # with a ? to ensure that the querystring gets to the server.
How can I achieve this in the browser, using JS?
Thanks in advance.
EDIT:
Just a few more thoughts on how the calls are made:
1) Call made 3rd party Identity Server endpoint
2) 3rd party Identity Server then redirects back, via browser, with provided URL ie localhost/endpoint with payload ie code=12345 usually ?code=12345 but in this case #code=12345 so full URL is localhost/endpoint#code=12345
3) At present localhost/endpoint gets sent back to server as everything post # gets stripped off, hence this question as to how I can correct localhost/endpoint#code=12345 to be localhost/endpoint?code=12345 and then forward to server
I am sorry about the down points, but I am limited here with my options
just use var fixedUrl = yourUrl.replace("#","?")
to forward inbound url, use location.href. Rude ducttape fix would be
if(location.hash.indexOf("=")>0) location.href.replace("#","?");
You can use replace if I understand you correctly.
const url = "localhost/endpoint#code=12345";
const newUrl = url.replace('#', '?'); // Replace # with ?
More information can be found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Node.js - How to create a request which can access the url before the root path "/'

I am trying to complete the freecodecamp project- url shortener in glitch.
glitch project link: https://glitch.com/edit/#!/url-sh0rtn3r
What i am able to do currently is:-
inside a POST request take the url to be shortened, convert it to a shorturl, add both to a database, and
inside a GET request search for the shorturl(appended to the url rootpath) and redirect to the original url...
the shortened url for https://www.freecodecamp.com
becomes https://url-sh0rtn3r.glitch.me/api/shorturl/9575 where 9575 points to the original url i.e. https://www.freecodecamp.com
Now i was wondering if i can somehow shorten the url further to be something like https://initialpart/api/shorturl/9575
But im stuck trying to figure out how to access the initialpart as a parameter inside a request where the path is pointing to whatever comes after initialpart.
You can get the "initialpart" (domain) from your request object with req.headers.host. In your case this would return "url-sh0rtn3r.glitch.me" as a string. You can then use that in your app however you like. You can also get the rest of the request URL path with req.url, which in the above example would return "/api/shorturl/9575" as a string.

Fetch Current URL not getting ID , class based filters

I am trying to fetch my current page URL, for an if-else statement.
My URL is like this : http://something.com/fetch_url.php#filter=.apples
And my code is :
<?php
echo $_SERVER['REQUEST_URI'].'<br/>';
?>
It gives me :
/fetch_url.php only, but i need to check for #filter=.apples , in if-else case.
Please guide.
Try using a correct URL like http://something.com/fetch_url.php?filter=apples
# part in the URL never approach to a web server, therefore you cannot access it.
And use if statement like this.
if($_REQUEST['filter']=='apples'){
//then perform your action here according to requirements
}else{
// otherwise provide instruction for the else condition here
}
A # part in the URL can never reaches a web server, hence you cannot access it. One workaround that we have been using is to use Javascript to access it via window.location.hash, and then do a separate post to the server to obtain this hash, if it's necessary.
Similar question has been asked before: Get fragment (value after hash '#') from a URL in php

How do I change pages and call a certain Javascript function with Flask?

I am not sure if I worded my question correctly. I'm not actually sure how to go about this at all.
I have a site load.html. Here I can use a textbox to enter an ID, for example 123, and the page will display some information (retrieved via a Javascript function that calls AJAX from the Flask server).
I also have a site, account.html. Here it displays all the IDs associated with an account.
I want to make it so if you click the ID in account.html, it will go to load.html and show the information required.
Basically, after I press the link, I need to change the URL to load.html, then call the Javascript function to display the information associated with the ID.
My original thoughts were to use variable routes in Flask, like #app.route('/load/<int:id>') instead of simply #app.route('/load')
But all /load does is show load.html, not actually load the information. That is done in the Javascript function I talked about earlier.
I'm not sure how to go about doing this. Any ideas?
If I need to explain more, please let me know. Thanks!
To make this more clear, I can go to load.html and call the Javascript function from the web console and it works fine. I'm just not sure how to do this with variable routes in Flask (is that the right way?) since showing the information depends on some Javascript to parse the data returned by Flask.
Flask code loading load.html
#app.route('/load')
def load():
return render_template('load.html')
Flask code returning information
#app.route('/retrieve')
def retrieve():
return jsonify({
'in':in(),
'sb':sb(),
'td':td()
})
/retrieve just returns a data structure from the database that is then parsed by the Javascript and output into the HTML. Now that I think about it, I suppose the variable route has to be in retrieve? Right now I'm using AJAX to send an ID over, should I change that to /retrieve/<int:id>? But how exactly would I retrieve the information, from, example, /retrieve/5? In AJAX I can just have data under the success method, but not for a simple web address.
Suppose if you are passing the data into retrieve from the browser url as
www.example.com/retrieve?Data=5
you can get the data value like
dataValue = request.args.get('Data')
You can specify param in url like /retrieve/<page>
It can use several ways in flask.
One way is
#app.route('/retrieve/', defaults={'page': 0})
#app.route('/retrieve/<page>')
def retrieve():
if page:
#Do page stuff here
return jsonify({
'in':in(),
'sb':sb(),
'td':td()})
Another way is
#app.route('/retrieve/<page>')
def retrieve(page=0):
if page:
#Do your page stuff hear
return jsonify({
'in':in(),
'sb':sb(),
'td':td()
})
Note: You can specify converter also like <int:page>

How to use `GET` parameters in a HTML document?

I have a HTML document and I want to send a parameter.
Let's name the document Categoty, and all it do it sends a request to the server (via Ajax) to get something on this categoty.
Suppose I'm using PHP, I would simply send the parameter:
localhost/Categoty.php?cat=somecat,
and read it in the PHP document: $_GET['cat'].
My question is if there's something similar in HTML (or Javascript)?
I though about hashtag (.i.e. categoty.html#somecat) , but I'm using jQuery mobile in my Android Hybrid application so it makes some issues.
Thanks in advance.
use the jQuery URL Parser in this link and then try:
jQuery.url().param('cat');
you can also access all other info we usually need in terms of dealing with GET parameters, like:
jQuery.url().attr('protocol');
returns the URL protocol like: http or https
jQuery.url().attr('host')
returns the host like stackoverflow.com
you can also get the segments of your URL like this:
jQuery.url().segment(1);//for instance in this very page's url it returns "posts"
at the end:
jQuery.url().param('cat') in javascript does the same as $_GET['cat'] does in PHP
jQuery ajax can do it , use $.ajax();
$.ajax({
type: 'GET',
url: 'http://localhost/Categoty.php?cat=somecat',
success: function(responseTxt,statusTxt,xhr){
}
});

Categories

Resources