Get redirect url after ajax GET [duplicate] - javascript

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;

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

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

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.

Categories