Unwrap t.co links using JavaScript? [duplicate] - javascript

This question already has answers here:
Find where a t.co link goes to [closed]
(7 answers)
Closed 10 years ago.
How can I get the target URL for a Twitter t.co link that has been captured via the Twitter API using JavaScript.
e.g. http://t.co/NJwI2ugt

I made a fiddle here: http://jsfiddle.net/duotrigesimal/XB8Uf/
It makes a request to the api at LongURL (http://longurl.org/api#expand-url) to get the expanded url.
I'm also using jQuery in this example, but you can make it work without if needed.
var tests = [
'http://t.co/NJwI2ugt',
'http://www.google.com' //nothing should happen
];
for(i in tests) {
var data = {
url: tests[i],
format: 'json'
};
$.ajax({
dataType: 'jsonp',
url: 'http://api.longurl.org/v2/expand',
data: data,
success: function(response) {
$('#output').append(response['long-url']+'<br>');
}
});
}

Related

Can I add /something to URL with JavaScript? [duplicate]

This question already has answers here:
How do I modify the URL without reloading the page?
(20 answers)
Closed 4 years ago.
Let assume I'm on page www.example.com. Can I add /first to the URL so that it becomes www.example.com/first on click WITHOUT refreshing the page. I'm trying to do this after an AJAX request which changes the page, so that I could indicate the page has been changed.
For example:
$('.admin-categories').on('click', function(){
$.ajax({
type: 'POST',
url : adminCategories,
headers: {
'X-CSRF-TOKEN': token
},
success : function (data) {
$(".admin-container").html(data);
}
});
});
Can I append /categories to the URL on success of this AJAX call?
You're looking for the History API in JavaScript and replaceState method specifically.
history.replaceState(null, null, 'first');
Here is a good resource about it:
https://css-tricks.com/using-the-html5-history-api

Receive data with flask and send data with jQuery [duplicate]

This question already has answers here:
How do I get the different parts of a Flask request's url?
(4 answers)
jQuery posting JSON
(3 answers)
How to get POSTed JSON in Flask?
(13 answers)
Closed 4 years ago.
!! THIS IS NOT A DUPLICATE !!
The question was not how to get an URL in Flask, but how to send data with jQuery to Flask!
I try to send and receive data with python, Flask and jQuery
The problem is that I want the full URL of the website and it is impossible to get it with flask because I make 'POST' requests. So with jQuery, I want to send the current URL.
I don't know how to send data (with jQuery) and receive data (with Flask).
Python/Flask code:
#app.route('/invisible', methods = ['POST'])
def dynamic_refresh():
return jsonify({'somedata': 'data'})
HTML/jQuery code:
<script>
$(document).ready(function() {
window.setInterval(function() {
$.ajax({
type : 'POST',
url : '/invisible',
//I tried to send data from here but it didn't worked
})
.done(function(data) {
console.log(data)
console.log(window.location.href)//the url I want to send
//here I use the data received by the server
})
}, 5000);
});
</script>
Its quite simple, enclose data in JSON array which you want to send through POST request and then retrieve any data from Flask endpoint like this;
var url = $('#url').val().trim(); //get your value from HTML here
var params = {
_url: url,
};
var array = JSON.stringify(params); //enclosed it in json array
$.ajax({
type: "POST",
url: "/invisible",
data: array,
dataType: 'json',
success: function(results){
console.log(results)
}
});

Scraping the url with Jquery [duplicate]

This question already has answers here:
Simple Screen Scraping using jQuery
(7 answers)
Closed 5 years ago.
I want to get data from other url which is product info. I want to scrape all of this data for this attribute:
$('[data-b-for-cart]').attr('data-b-for-cart');
And want to export that to csv file.
Not sure hows this should be done any resource would be helpful.
I think I should use the jquery $.get is that right ?
you can try ajax within jquery to scrape. It is not that difficult
$(document).ready(function() {
baseUrl = "http://www.somedomain.com/";
$.ajax({
url: baseUrl,
type: "get",
dataType: "",
success: function(data) {
//do something with data and save as csv file
}
});
});

Passing JQuery var to PHP using AJAX [duplicate]

This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 7 years ago.
I'm trying to pass 3 variables that look like this
var location = document.location.pathname;
var search = document.location.search;
var referrer = document.referrer;
Into a PHP file that I can eventually use to send emails, I have never used AJAX before but know that you could use it to achieve this.
Can anyone help me out? Thanks in advance.
A simple Ajax POST method can help you here. Here's an example.
$.ajax({
type: "POST",
url: "ajax.php",
data: {location: location, search: search, referrer: referrer},
success: function(response){
//do something
}
})//ajax end
Now in ajax.php, you can receive the values via $_POST.
PHP Receiving(ajax.php).
var_dump($_POST['location']);
You could do like this:
$.ajax({
type: "POST",
data: {'location': location,
'search': search,
'referrer': referrer
},
url: "Here the path to your php-file",
success: function (data) {
Here you could react on any
}
});
In the php file you receive those data by Post and can handle them.

How to load the html content of a URL to a variable in JavaScript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
HTTP GET request in Javascript?
I have a url that contains a XML tree. I want to read that url content, save it to a variable, extract XML tree values, and draw it in a table.
Use an AJAX query, in jQuery:
$.ajax({
url: url,
dataType: 'xml',
success: function(data) {
// Data is your xml content which you can work with
}
});

Categories