Click here to Skip to main content
11,410,118 members (35,308 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript Ajax
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

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 12-Dec-12 23:25pm
Ganeshcse2.4K
Comments
Christian Graus at 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 ?
Ganeshcse at 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 at 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?
Ganeshcse at 17-Dec-12 2:36am
   
How can we know whether a response or request is happened? is this is possible with firebug huh?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello, my be you have missing the parameter passing

success: OnSuccessGetAllItemsOnLoad,
            error: OnFailOfGetAllItemsOnLoad

you can try the correct code like this

success: OnSuccessGetAllItemsOnLoad(response),
            error: OnFailOfGetAllItemsOnLoad(msg)
  Permalink  
Comments
Ganeshcse at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
Ganeshcse at 17-Dec-12 2:38am
   
also tried this type , but still same problem persists
Md. Humayun Rashed at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 219
1 Maciej Los 128
2 OriginalGriff 120
3 Sascha Lefévre 120
4 BillWoodruff 60
0 Sergey Alexandrovich Kryukov 8,771
1 OriginalGriff 6,905
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100