Click here to Skip to main content
15,881,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends, I have small doubt in ajax function in asp.net

I have one ajax function which is called on Page Load of Site1.Master so that I can use that result out come from the function in all pages that inherit from site1.master.

So I have placed that ajax function in separate js file and included that file in my site1.master at first place before other js files to inculde.

When that site1.master loads (most probably after the user login) I am able to go to the function, but I am unable to go to either "Success" or "error" functions. so I have added a attribute "async:false". still the problem is persists.

Please any one can help me.

Here is my ajax code in ap.js file

C#
var AllItems = new Array();

function GetAllItemsOnLoad() {
    var aid = 1;
    var param = '{"AgencyId":"' + aid + '"}';
    try {
       $.ajax
        ({
            type: "POST",
            async: false,
            contentType: "application/json; charset=utf-8",
            url: WebSvcUrl + "/GetAllItems",
            data: param,
            dataType: "json",
            success: OnSuccessGetAllItemsOnLoad,
            error: OnFailOfGetAllItemsOnLoad
        });
    }
    catch (err)
    { alert("Unable to get the data, Please Try again"); }
} // of Get All Items

function OnSuccessGetAllItemsOnLoad(msg) {
    try
    {
        if (msg.d != null)
        {
            AllItems = msg.d;
        }
        else
        {
             alert("Unable to retrive data, Please Try again");
        }
    }
    catch (err)
    { alert("Unable to get the data, Please Try again"); }
} // on func succ get all items

function OnFailOfGetAllItemsOnLoad(request, status, error) {
    $("#lblErrIname").html("Unable to get the data, Please Try again");}

Here is the code I am calling in my site1.Master page

  if (LoginUser == 3)
 {
            ShowAdminDiv();
            GetAllItemsOnLoad();
 }
 

Please any one Could tell me where I am wrong.

Thanks and Regards
Ganesh
Posted
Comments
Christian Graus 13-Dec-12 4:27am    
When you say 'on page load' you mean inside the aspx, in javascript, right ? Have you added some alerts or debugged the code in chrome to see what's happening and if the call is made ?
Ganesh KP 13-Dec-12 22:33pm    
Yes it is on page load using javascript i.e $(document).ready(function())....not only I am using this function on page load but also in some Onclick events for HTML buttons. in either way I am unable to go to either success or error functions but when I keep a break point at the start of the fun yes, the controls is going to the pass the break point. and another problem is that it is working only in some browsers, I dont understand why this is happening and I am getting doubt whether the procedure that I followed is correct or not to call a web service using ajax in javascript.
rahkan 15-Dec-12 8:43am    
Have you used chrome or firebug to check that it is actually making a POST request to the server and that it is getting a response? If so, what is the server response?
Ganesh KP 17-Dec-12 2:36am    
How can we know whether a response or request is happened? is this is possible with firebug huh?

Hello, my be you have missing the parameter passing

VB
success: OnSuccessGetAllItemsOnLoad,
            error: OnFailOfGetAllItemsOnLoad


you can try the correct code like this

VB
success: OnSuccessGetAllItemsOnLoad(response),
            error: OnFailOfGetAllItemsOnLoad(msg)
 
Share this answer
 
Comments
Ganesh KP 13-Dec-12 22:35pm    
No Boss, it works long back when I worked with some project in the similar manner, but this time it is giving a lot of trouble. and I tried also in the same way what u said but still the problem persists. I hope u understand my problem clearly. if don't Please again post for new comment
JavaScript
var AllItems = new Array();
            function GetAllItemsOnLoad() {
                
                var aid = 1;
                var param = '{"AgencyId":"' + aid + '"}';
               
                      $.ajax({
                            type: "POST",
                            url: "DocumentUpload.asmx/GetAllItems",
                            data: param,
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                alert(response.d);
                            },
                            failure: function (msg) {
                                alert(msg);
                            }
                        });

                } // of Get All Items

follow this.
 
Share this answer
 
v2
Comments
Ganesh KP 17-Dec-12 2:38am    
also tried this type , but still same problem persists
[no name] 17-Dec-12 3:10am    
then may be you have another problem. but you can follow this article to clear conception of JSON and WebService:

http://www.codeproject.com/Articles/498591/Client-side-data-grid-with-JSON-and-Web-Service

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900