Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-30-2011, 12:53 PM   PM User | #1
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
$.ajax success: function issue

Hi all

I'm building myself an ajax login and decided to use jQuery for the ajax part.

The part I'm struggling with is the success: function. I am trying to pass a function as a variable to run on success, but I'm having a few issues. It seems to kinda work in FF but Chrome doesn't like it one bit.

Here's what I've got:
Code:
function call_ajax(request_page, params, rewite_div, method, working_message, success_function) {
	
	$.ajaxSetup ({
		cache: false
	});
	$.ajax({
		statusCode: {
		404: function() {
				document.getElementById('alertHeader').innerHTML = '404 Error';
				document.getElementById('alertTxt').innerHTML = '<p>The page was not found</p>';
				open_error('#alertDiv');
			},
		401: function() {
				document.getElementById('alertHeader').innerHTML = '401 Unauthorized';
				document.getElementById('alertTxt').innerHTML = '<p>Authorization required</p>';
				open_error('#alertDiv');
			}
		},
	  type: method,
	  url: 'includes/ajax/'+request_page, 
	  data: params,
	  beforeSend: function () {
		// this is where we append a loading image
		$(rewite_div).html('<img src="images/spinner.gif" alt="Loading..." /> ');
		$(rewite_div).append(working_message);
	  },
	  success: function (data) {
		// successful request; do something with the data
		var obj = jQuery.parseJSON(data);
		if(obj.result == 'fail'){ 
			$(rewite_div).empty();
			var clean_str = str_replace(obj.reason,"|",'"'); 
			$(rewite_div).html(clean_str);
		}else{
			success_function; // this is where my problem is
			
		}
		  
	  },
	  error: function() {
		// failed request; give feedback to user
		$(rewite_div).html('Oops, something went wrong...');
	  }
	});
	
}

Now I'm passing these vars in from my login() function:

Code:
var success_function = function(){locate_to('home.php')};
var params = new Object();
params.username = document.getElementById('username').value;
params.password = $.sha1(document.getElementById('password').value);
		call_ajax('login.php',params,'#result_div','POST',' Logging you in...', success_function);
And the function that's actually trying to be called is:

Code:
function locate_to(url){
	location.href = url;
}
Now if the user has entered invalid data the json result will be fail, otherwise it's a pass, this part of the 'success' call runs fine, it just doesn't like the success_function var, it just doesn't fire.

I don't get any errors in FireBug, which doesn't help me as I can't see what's going wrong, so I don't think it's a syntax issue.... or I might be wrong.

Does anyone know how to pass a function as a variable to this part of a jQuery ajax call?

Any advise or pointers will be greatfully received as always

EDIT:
I forgot to mention that I would like to keep this call_ajax function as generic as possible so I can reuse this one function for all my ajax requests

Last edited by loki421; 09-30-2011 at 12:55 PM.. Reason: Forgot to add something.. :p
loki421 is offline   Reply With Quote
Old 09-30-2011, 01:04 PM   PM User | #2
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
Cracke it!

Turns out I needed to wrap the success_function like so:

Code:
            success: function (data) {
		// successful request; do something with the data
		var obj = jQuery.parseJSON(data);
		if(obj.result == 'fail'){ 
			$(rewite_div).empty();
			var clean_str = str_replace(obj.reason,"|",'"'); 
			$(rewite_div).html(clean_str);
		}else{
			$(success_function);
			
		}
		  
	  }
Hope this helps someone
loki421 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:53 AM.


Advertisement
Log in to turn off these ads.