syncronous ajax request and variables outside of the request
I've built a function that handles my ajax calls. I know it doesn't make much sense in this small test script i built, but i would really like to have this work in this way. I'll explain the script then post it below.
First, i have a function that creates a dialog based on the parameters passed in. I simplified the function for this test to only accept one parameter; an ajax options object. Inside of this function, i am creating a default ajax options object, extending it with the ajax options object passed in, I then initialize the return variable with a string, and make the ajax call. Within the success function of the ajax call, i set the value of the return variable equal to the jquery object. After this ajax call, i alert the return variable. If successful, the return variable should be an object instead of a string.
Temp2.cfm returns:
- <div></div>
the script:
- $(document).ready(function(){
- var sendAjaxRequest = function(ajaxParam) {
- var defaultAjax = {
- method:"GET",
- async:false,
- success:function(data){
- returnData = data;
- }
- };
- $.extend(defaultAjax,ajaxParam);
- var returnData = "default";
- $.ajax(defaultAjax);
- alert(returnData);
- }
- $('a.clicker').click(function(){
- var customAjaxObj = {
- url: "temp2.cfm",
- success: function(data){
- returnData = $(data).attr('name','customName');
- }
- }
- sendAjaxRequest(customAjaxObj);
- });
- });
Edit: i forgot to mention what was happening. the alert always returns "default" rather than the object.
-- Kevin
------------------------------------------------
http://www.tentonaxe.com