Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
whats the problem in this webmethod/webservice call
JavaScript
$.ajax({
type: "GET",
url: "jquerygrid.aspx/GetCustomers",
dataType: "json",
success: function(resp){
// we have the response
alert("Server said123:\n '" + resp+ "'");
},
error: function(e){
alert('Error121212: ' + e);
}
}); 


GetCustomers is name of my method in jquerygrid.aspx page.
i also tried to call a webservice method like autocomplete.asmx/getcustomers.

Following is my webmethod

[System.Web.Services.WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetCustomers()
        {
            string query = "SELECT * FROM table";
            SqlCommand cmd = new SqlCommand(query);

            // Populate the DataSet.
            DataSet data = GetData(cmd);
            string strjson = GetJson(data.Tables[0]);
            return strjson;
        }
Posted
Updated 14-Jan-14 20:20pm
v5
Comments
Do you see any Errors in Developer Tool's console?
pwavell 15-Jan-14 2:38am    
alert box with Error [object Object]
So, is it going inside Success or Error?
pwavell 15-Jan-14 2:44am    
Error
Are you sure that the jQuery code is in jquerygrid.aspx and GetCustomers is present in its code behind?

You have to add this line to you ajax call
JavaScript
contentType: 'application/json; charset=utf-8',

(Just after dataType...)
 
Share this answer
 
Add data attribute and change type to Post in the jquery ajax call
 
Share this answer
 
Comments
pwavell 15-Jan-14 2:53am    
using this:=
$.ajax({
type: "Post",
url: "jquerygrid.aspx/GetCustomers",
dataType: "json",
data: "{}",
async:false,
contentType: "application/json; charset=utf-8",
success: function (response) { // we have the response
alert(response);
},
error: function(e){
alert('Error121212: ' + e);
}
});
still giving error
JoCodes 15-Jan-14 3:56am    
when ajax call parameter type:post then in the webmethod use UseHttpGet = false.
Add async:false to your ajax method and make sure your your calling page and service method page in same directory.

Add also following code in ajax method

data: "{}",

contentType: "application/json; charset=utf-8",

type: "POST",
 
Share this answer
 
Comments
pwavell 15-Jan-14 2:48am    
still going into error block.(Error [object Object])
Sandeep Singh Shekhawat 15-Jan-14 2:54am    
Is Your web service method not hitting?
pwavell 15-Jan-14 3:00am    
yes.cant debug my webmethod.
Sandeep Singh Shekhawat 15-Jan-14 3:22am    
Your are using UseHttpGet = true in web Methdod so please use type: "Get", in jQuery Ajax method.
pwavell 15-Jan-14 3:55am    
Now i am able to debug my web method but its alert is giving me [object Object] but not data.
jquery call :
JavaScript
$.ajax({
    type: 'POST',
    url: 'AddProgressBar.aspx/GetUpdateStatus',
    cache: false,
    async: true,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response, type, xhr) {
         var retVal = JSON.stringify(response);
        alert("hiiiiiiiii");
//                            alert(response.d);
        window.alert(JSON.parse(retVal).GetJSONDataResult);
    },
    error: function (xhr) {
        window.alert('error: ' + xhr.statusText);
    }
});

cs file ::
C#
[WebMethod]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "json")]
public static string GetUpdateStatus()
{
    return GetUserAddStatus();
}
 
Share this answer
 
v2
Comments
er.rakesh 15-Jan-14 4:05am    
plz do let me know...
pwavell 15-Jan-14 4:47am    
working...thanks a lot
er.rakesh 15-Jan-14 4:52am    
gud..
why have u devoted my answer ??

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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