Django template code not executing - javascript

I already defined a handler for "get_list" but whenever I run this AJAX code, the terminal does not show that I have called this "get_list" function. Please help.
<script>
function update_list() {
$.ajax({
url: "/get_list/",
type: "POST",
data: {'csrfmiddlewaretoken': $.cookie("csrftoken"), 'type': 'open'},
success: function(data) {
alert("PUSHED")
$('.ticketList').html(data);
}
});
}
$(document).ready(function() {
update_list();
});
</script>

Related

how to execute a javascript function returned from ajax response

I have a function which i want to execute when ajax makes a successful request but it doesn't execute here my jquery part
$.ajax({
type: "POST",
url: "https://www.example.com/create_chat.php",
data: dataString,
beforeSend: function()
{
$("#loading_indicator").show();
},
success: function(response)
{
$(".example").html(response);
}
});
here is the response from php file
<script>start_chat(); alert("testing");</script>
i tried adding this also
$(".example").find("script").each(function(i) {
eval($(this).text());
});
but nothing works
Your response from create_chat could indicate which function will be used. For example
$.ajax({
type: "POST",
url: "https://www.example.com/create_chat.php",
data: {
dataString: dataString
},
beforeSend: function()
{
$("#loading_indicator").show();
},
success: function(response) // response could be 1,2,3,4.. etc
{
if(response==1) {
start_chat();
}
if(response==2) {
stop_chat();
}
if(response==3) {
change_chat_room();
}
...
$(".example").html(response);
}
});
While there are work arounds, you should never be executing the response of an Ajax call directly.
"Scripts in the resulting document tree will not be executed,
resources referenced will not be loaded and no associated XSLT will be
applied."
http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body
Best practice is to respond with data, typically in the form of JSON, JSONP or HTML.
Try This,
$.ajax({
type: "POST",
data: {
'dataString': dataString
},
url: 'https://www.example.com/create_chat.php',
success: function(data) {
start_chat();
alert('Success');
},
error: function(data) {
alert('failed');
}
});

Ajax redirect on error

I'm calling php file through ajax call and if it returns nothing i want to redirect user to another page (It's for error reports, if it doesn't return anything it means that user logged in). Tried to add error section but it doesn't work. Any suggestions will help. Thanks! Btw, I have small jQuery function at the top of the ajax function, why it breaks my whole ajax call?
ajax.js
function loginAjax() {
//$("#email_errors").empty(); //This function doesnt work and kills whole ajax call. Here is loginAjax function call line - <button type = "submit" id = "push_button" onclick = "loginAjax(); return false">PushMe</button>
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
$("#login_error").html(data.login_message);
}
});
}
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
$("#login_error").html(data.login_message);
},
error: function(){
window.location.replace("http://stackoverflow.com");
}
});
}
To redirect using javascript all you need to do is override the location.href attribute.
function loginAjax() {
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
// the success method is deprecated in favor of done.
done: function(data) {
$("#login_error").html(data.login_message);
},
fail: function(data) {
location.href="path/to/error/page";
}
});
}

simple PHP AJAX : How to call a php script

I'm trying to get my javascript function to use ajax to call a php script. The php script will update a MYSQL table but I've tested the PHP script and it works fine.
This is my function:
function rate()
{
$.ajax({
data: '' ,
url: 'update.php',
method: 'GET',
success: function(msg) {
alert(msg);
}
});
}
and the function is called later with:
rate();
The php script doesn't need any information given to it, it just needs to be called, can anyone point out where I'm going wrong.
Just use like in this example:
<script>
function rate() {
$.get("update.php");
}
</script>
If you dont need to pass the data to the PHP page, you can omit 'Data' from the ajax parameters.
<script>
function rate(){
$.ajax({
url: 'update.php',
method: 'POST',
success: function(msg) {
alert(msg);
}
});
}
rate();
</script>
Have you included jquery library
Try this
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
function rate()
{
$.ajax({
data: '' ,
url: 'update.php',
method: 'GET',
success: function(msg) {
alert(msg);
}
});
}
rate();
</script>
Here is an example of how I use the ajax call:
$.ajax({
type: "POST",
url: "page_url",
dataType: 'json',
data: {
'date1' : date1,
'call': 'function_name'
},
beforeSend: function(){
$("#loading").show();
},
success : function(response){
},
complete: function(){
$("#loading").hide();
}
})
and on php part I add:
function function_name($request){
your code here
}
if (!empty($_POST['call'])) {
die($_POST['call']($_POST));
}
i show my all working code, try it:
<html>
<head>
<script type="text/javascript" src="/js/jquery-1.8.2.js"></script>
<script>
function rate() {
$.get("update.php");
}
</script>
</head>
<body>
<button onclick="rate()">Click me</button>
</body>
</html>

Jquery Ajax call inside Jquery Ajax call

I'm loading a part of a page using ajax with this script:
<script type="text/javascript">
$(document).ready(function(){
$('#btn_new').live('click',function(){
var parameter = 1;
$.ajax({
type: "POST",
url: "new_message.php",
data: {
parameter:parameter,
},
success: function(msg)
{
$("#lib_container").ajaxComplete(function(event, request, settings)
{
$("#lib_container").html(msg);
});
}
});
});
});
</script>
Inside the lib_container div I have a text input with this script:
<script type="text/javascript">
function searchUsers(str) {
$.ajax({
type: "POST",
url: "users_ajax.php",
data: {
search:str
},
success: function(msg)
{
//console.log(msg);
//$("#users_lst").html(msg.response);
$("#users_lst").ajaxComplete(function(event, request, settings)
{
$("#users_lst").html(msg);
});
}
});
}
</script>
When I load some text in the input, the second ajax script loads the msg variable in the div but then also reloaded the lib_container div.
I've tried many ways to prevent the lib_container ajax trigger but it was in vain. Just putting the input in new page works correctly.
Can anyone help me solve this?
Thanks.
Your problem seems to be that you are registering a new ajaxComplete handler every time you get a successful response from the ajax call. Since it seems like you want to update the html, I'm not sure why you need those ajaxComplete handlers anyway.
Try it like this:
$(document).ready(function(){
$('#btn_new').live('click',function(){
var parameter = 1;
$.ajax({
type: "POST",
url: "new_message.php",
data: { parameter:parameter },
success: function(msg) {
$("#lib_container").html(msg);
}
});
});
});
And by the same token:
function searchUsers(str) {
$.ajax({
type: "POST",
url: "users_ajax.php",
data: { search:str },
success: function(msg) {
$("#users_lst").html(msg);
}
});
}
On another note, .live is deprecated and you really ought to be using ajax promises rather than a success callback. See the jQuery api for more info on that.

When i use the window.close ajax call not working?

I have the small window in my page when click the button the window value send my controller and close the window. But when i use the window.close the controller not hit. But without window.close its hits properly. But i need that line for close the window.How i manage that?
My sample Code is give below:
function click() {debugger;
$.ajax({
type: 'POST',
url: 'my url'
data: { status: $('#Field_Error')[0].checked },
dataType: 'json',
success: function (data) {
alert(data.message);
}
});
window.close();
}
controller:
public ActionResult actionName(bool status)
{// not hit here
}
Call window.close from inside success function
$.ajax({
type: 'POST',
url: 'my url'
data: { status: $('#Field_Error')[0].checked },
dataType: 'json',
success: function (data) {
alert(data.message);
window.close();
}
});
Ajax is async in nature. It does not wait for ajax to complete. So window.close called early w/o completing ajax.
put the window.close in succes, jquery doesn't wait for ajax to finish, it goes on.
function click() {debugger;
$.ajax({
type: 'POST',
url: 'my url'
data: { status: $('#Field_Error')[0].checked },
dataType: 'json',
success: function (data) {
alert(data.message);
window.close();
}
});
}
function click() {
var myurl=test.php;
$.ajax({
type: 'POST',
url: 'myurl',
data: { status: $('#Field_Error')[0].checked },
dataType: 'json',
success: function (data) {
alert('successmsg');
window.close();
}
});
}

Categories