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

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

Related

Calling ajax request after processing another ajax request [duplicate]

This question already has answers here:
How to manage a redirect request after a jQuery Ajax call
(34 answers)
replace div content using ajax and jquery
(4 answers)
Replace HTML page with contents retrieved via AJAX
(7 answers)
Closed 5 years ago.
User pushes the button on site and by this ajax request starts than Server returns True or False. If the result is True than another ajax request is to be processed, but I am getting nothing (I guess inside of ajax).
Here is my js code:
document.getElementById('next_in').onclick = function (){
$.ajax({
data: {
login: document.getElementById('log').value,
password: document.getElementById('pass').value
},
type: 'POST',
url: '/user_login',
success: function (localRes) {
if(localRes.result==true){
$.ajax({
data: {
login: document.getElementById('log').value
},
type: 'POST',
url: '/private',
success: function () {
alert('Connected')
},
error: function () {
alert('There is a mistake!')
}
});
}
else{
alert('Incorrect login or password!');
}
}
});
}
and python code:
#app.route('/private', methods=['POST'])
def private():
return render_template("rates.html")
Then after pushing the button on site I recieved "Connected", but then (I supposed this event calls my python function) there is no redirect to rates.html...
I do not understand what is wrong here..
Please. I hope at leaste to understand problem of which side it is and how to fix it?
Thank you!
EDIT ONE
I did shorten my python function just to show the issue. In actual case before return render_template("rates.html") there is huge proccessing (request to database, some calculation and so on), so python function is:
#app.route('/private', methods=['POST'])
def private():
# ******************** HUGE processing
return render_template("rates.html")
Sorry, if I confused you, but simple redirect to .html is not what I want. I want calling python function in nested ajax requests.
When you use an AJAX request, the browser doesn't automatically redirect you, or display any content that is returned from your request.
If rates.html is a full HTML page, change your inner callback from
success: function () {
alert('Connected')
},
to this:
success: function(data) {
document.body.innerHTML = data;
},
That takes the response from the server (your python code), and then does something with it (in this case renders it on the browser).

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
}
});
});

Get redirect url after ajax GET [duplicate]

This question already has an answer here:
jquery ajax form - how to get the redirect url?
(1 answer)
Closed 8 years ago.
Hi I am saving a form using jquery ajax.
$.ajax({
type: "POST",
url: /admin/department/save,
data: $(".formstyles").serialize(),
success: function(data, status,xhr) {
$(contentHolder).html(data);
}});
When this url get a hit it saves the form and redirects to /admin/department/edit/1090
Here, the ajax get is processed and response is received.
I want redirected url from request headers, somehow.
after success you can redirect url
window.location.href = URL;

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
}
});

Unwrap t.co links using JavaScript? [duplicate]

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>');
}
});
}

Categories