locked
ajax call to shared (static) method RRS feed

  • Question

  • User-1997257017 posted
    I am having a problem with an ajax call to a shared (static) vb.net function to obtain data from the database.
    In a vb.net 2010 web form application, I needed to make a call from a javascript function to a shared (static) function to
    obtain data from a sql server 2012 database. The first ajax call below is the original call and the second call is the one
    I came up by copying the first call.
    The problem is the first call always occurs before the second ajax call regardless of what I do with the code. There are cases when I
    only want the second call to occur. By stepping through the code, I see that there is alot of jquery code that occurs.
    $.ajax({ 
                        type: "POST", 
                        contentType: "application/json; charset=utf-8", 
                        url: document.location.pathname + "/GetStudentLetter", 
                        data: "{ " + strData + " }", 
                        dataType: "json", 
                        success: function (data) { 
                            LoadEditor(JSON.parse(data.d), milestone); 
                        }, 
                        error: AjaxFailed 
                    });            
                if (milestone == '999') { 
                    $.ajax({ 
                        type: "POST", 
                        contentType: "application/json; charset=utf-8", 
                        url: document.location.pathname + "/Gettattschyr", 
                        data: "{ " + strData + " }", 
                        dataType: "json", 
                        success: function (data) { 
                            successtest(eval(data));                       
                        }, 
                        error: AjaxFailed 
                    }); 
                }                
             }           
            function successtest(sletter) { 
                var sle = sletter 
                _txtattschyr = sletter.d 
                if (_txtattschyr > '1') { 
                    alert('You have selected student(s) having more than one Entry for the current school year. '); 
                    return                 
                }               
            } 
    
    Thus would you tell me what you think is wrong and/or tell me how I can change the second ajax call to try to solve this issue?   

    Saturday, August 27, 2016 1:42 PM

Answers

  • User475983607 posted

    wendy elizabeth

    I tried to do what you are saying, but I kept getting an error message saying there was a JavaScript error when debugging

    And the error is?  And your code?

    wendy elizabeth

    I also got an error message saying it could not find the JavaScript function that I was trying to call.

    This is usually a scope issue.

    wendy elizabeth

    When I run the original ajax call, I am finding that there are lots jquery functions in the background that may be causing this problem.

    I'm not sure what the previous statement means or its relevance to the current issue.

    wendy elizabeth

    Thus could you show me an example of how I can change the code I originally included?

    The AJAX method would something like this.

    $.ajax({ 
    		type: "POST", 
    		contentType: "application/json; charset=utf-8", 
    		url: document.location.pathname + "/GetStudentLetter", 
    		data: "{ " + strData + " }", 
    		dataType: "json", 
    		success: function (data) { 
    			LoadEditor(JSON.parse(data.d), milestone); 
    			if (milestone == '999') { 
    			$.ajax({ 
    				type: "POST", 
    				contentType: "application/json; charset=utf-8", 
    				url: document.location.pathname + "/Gettattschyr", 
    				data: "{ " + strData + " }", 
    				dataType: "json", 
    				success: function (data) { 
    					successtest(eval(data));                       
    				}, 
    				error: AjaxFailed 
    			}); 
    		} 
    		}, 
    		error: AjaxFailed 
    	}); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 27, 2016 5:27 PM

All replies

  • User475983607 posted

    AJAX is asynchronous which loosely means you don't know when the response will come back. Therefore, you create a handler (success) that processes the response when it comes back.  Simply move the second AJAX method to the "success" handler of the first AJAX method.

    Remember to use Developer Tools when debugging client side script.

     

    Saturday, August 27, 2016 3:54 PM
  • User-1997257017 posted

    I tried to do what you are saying, but I kept getting an error message saying there was a JavaScript error when debugging. I also got an error message saying it could not find the JavaScript function that I was trying to call. When I run the original ajax call, I am finding that there are lots jquery functions in the background that may be causing this problem.

    Thus could you show me an example of how I can change the code I originally included?

    Saturday, August 27, 2016 5:16 PM
  • User475983607 posted

    wendy elizabeth

    I tried to do what you are saying, but I kept getting an error message saying there was a JavaScript error when debugging

    And the error is?  And your code?

    wendy elizabeth

    I also got an error message saying it could not find the JavaScript function that I was trying to call.

    This is usually a scope issue.

    wendy elizabeth

    When I run the original ajax call, I am finding that there are lots jquery functions in the background that may be causing this problem.

    I'm not sure what the previous statement means or its relevance to the current issue.

    wendy elizabeth

    Thus could you show me an example of how I can change the code I originally included?

    The AJAX method would something like this.

    $.ajax({ 
    		type: "POST", 
    		contentType: "application/json; charset=utf-8", 
    		url: document.location.pathname + "/GetStudentLetter", 
    		data: "{ " + strData + " }", 
    		dataType: "json", 
    		success: function (data) { 
    			LoadEditor(JSON.parse(data.d), milestone); 
    			if (milestone == '999') { 
    			$.ajax({ 
    				type: "POST", 
    				contentType: "application/json; charset=utf-8", 
    				url: document.location.pathname + "/Gettattschyr", 
    				data: "{ " + strData + " }", 
    				dataType: "json", 
    				success: function (data) { 
    					successtest(eval(data));                       
    				}, 
    				error: AjaxFailed 
    			}); 
    		} 
    		}, 
    		error: AjaxFailed 
    	}); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 27, 2016 5:27 PM