im trying to run javascript function when other funcion will stop.
For example: User change something in form and ajax has been sent in background. When ajax request is done i want to run function.
How can i do that ?
Or maybe there is a global trigger for ajax requests ?
You could achive that thanks to ajax asynchronous request and handler. If you, for example, call via ajax a page that do a particular function and then return, you can run your js.
Take a look at this: http://api.jquery.com/ajaxComplete/
You can configure a handler for the ajax request which gets called when you get response for the request. You can check the examples at http://api.jquery.com/jQuery.ajax/
If you want to call a specific function for a specific AJAX call, simply call it as part of the success or complete callbacks on that AJAX call. If, however, you want to call the same function whenever any AJAX request is finished, take a look at the .ajaxSuccess() or .ajaxComplete() methods in the jQuery API.
It looks that you are using jQuery, so you can use the "complete" or "success" settings to call code/function. Check the docs.
$.ajax({
url: "test.html",
success: function(){
// call another function
}
});
Related
It's my first time using AJAX and I don't understand why I need to specify url parameter in a JS Ajax call.
{% block javascript %}
<script>
$("#id_username").change(function () {
$.ajax({
url: '/some_new_url/',
data: {
'something': ...
},
success: function (data) {
if (data.is_taken) {
alert("Data is already in DB");
}
}
});
});
</script>
{% endblock %}
To my understanding, AJAX is used to do something on the server side without refreshing a page. So it shouldn't redirect to a new url upon sending a data to the server, and stay on the same url. And yet AJAX call requires url parameter.
And I don' really like this, because setting a new url means I have to add another url pattern in my app/urls.py.
re_path(r'^create/$', views.Some_View.as_view(), name='create'),
And as a consequence, make another view in my views.py
class Some_View(ListView):
model = SomeModel
fields = '__all__'
But, I already have a CBV that generates form fields on the user side and accepts user inputs. I only want to make my existing CBV to save data to DB using AJAX call.
Since I don't understand what the purpose of the url is, I don't know how to set up my new url pattern, and CBV. Can I get some explanation here?
++ This is just a bonus question, but my ultimate goal is to generate multiple form fields, and multiple Submit buttons that sends the respective form input data to the server using AJAX. If there's any advice on how to tweak AJAX code, I would appreciate it.
An AJAX request is just a regular HTTP request to a url on the server. The only difference between an AJAX request and a request made by an ordinary browser GET or POST is that with AJAX, the results that come back from the server are returned to your javascript function and then you get to decide what to do with those results.
So there's no automatic updating of anything.
If you want to save something on the server, you need a view there on the server which is capable of understanding the data you are sending in the AJAX request, saving it, and then sending back a response which, again, your javascript code needs to be able to understand.
But if you already have a view which is capable of doing what you want, you can use it for your AJAX request, you just have to send a request with everything in it that the view requires.
If I am going to encrypt the data being passed by jQuery ajax prior to sending to the network (regardless if the network is SSL'd or not), where can I inject that functionality?
You can intercept all ajax queries initiated by jQuery, examine their content and change them (encrypt in your case) by using ajax prefilter in jQuery. With is approach you can modify all requests' contents at one place globally.
Details: http://api.jquery.com/jQuery.ajaxPrefilter/
Well, jQuery.ajax has a data field, which corresponds to the data sent in your request. So cleanest would be to set this field as a call to your encoding function.
$.ajax({
...
data: yourDataEncodingFunction(),
...
});
Remember data must be Key/Value pairs, so be sure that's what your function returns.
In a scenario, I have written a javascript and in middle of the script I made an AJAX call. There is some more amount of script which is present after the AJAX call and this needs to be executed based on the results returned from the function. But Unfortunately the script which is present after the AJAX call is executed before the function call itself, that is, the call is being made asynchronously. Could anyone please suggest me how to overcome this problem?
Thanks in Advance..
Are you using jQuery? IF not, you should :)
Then you should read this documentation - http://api.jquery.com/jQuery.ajax/
And put the rest of the script in the success handler of the ajax function.
If you still insist on not using jQuery, just use the onreadystatechange function
http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
I am trying to understand how the Yii pagination for posts works in the Blog demo app and I see this request in the firebug console:
http://localhost/blog/index.php/post/index?ajax=yw0&Post_page=2
The function 'actionIndex' in the PostController does not seem to use the $_GET params. Where does the magic happen?
For such things you should check the source.
The index function would have a CActiveDataProvider whose fetchData function does this work.
Basically a CListView, or a CGridView calls the getData function of a data provider, which calls fetchData (say of CActiveDataProvider), which in turn calls CPagination's applyLimit, which calls getOffset, and this function calls getCurrentPage:
if(isset($_GET[$this->pageVar])) // this is where the $_GET is used
so still a newb to nodeJS and back end code in general. I need some help with ajax and node. For instance I have a function that goes like this
function random(response) {
var objToJson = {...};
response.write(objToJson);
response.end();
}
if instead of writing I want to pass this json object to another function as a response of an ajax call made to it how would that be?
Thanks for you help!
Node.js allows you to easy manipulate HTTP request and response objects.
Ajax still sends a HTTP request object and the Ajax onsuccess callback manipulates a HTTP response object.
Writing an object to a response for an ajax request allows your ajax success handler to manipulate that data.
There are abstraction libraries for RPC like now
It sounds like you want to return a javascript object to work with in your client-side code. While that's not possible directly (you can't send an object directly over HTTP; you're always serializing/deserialing in some fashion), you can certainly return a JSON payload and easily convert that to an in-memory javascript object. If that's what you're doing, you should set the response content type to application/json.
response.setHeader("Content-Type", "application/json");
If you're writing "pure" javascript (no framework wrapping XmlHttpRequest), you'll need to eval() the responseText to convert it to an object. If you're using something like jQuery, it will do that work for you (assuming you set the content type as suggested above).