views:

568

answers:

4

Hi there,

Can anyone help? I have an issue with calling a asp.net webservice from jquery.. actually i think it maybe jquery ... as i have a break point and it doesn't arrive in the webservice..

Here is my jquery, the webservice method accepts 2 parameters...

So i setup a simple test to pass in 7 and 7 .. i tried replacing with the word "test" also and it doesn't work..

Basically lands in the error function which displays "sorry error happens" but the err is undefined.

jQuery.ajax({
         type: 'POST'
            , url: 'CallService.asmx/TempCanMakeCall'
            , contentType: 'application/json; charset=utf-8'
            , dataType: "json"
            , data: "{'reservationNum':'7','completedReservationNum':'7'}"
            , success: function(data, status) {
                alert(data);
            }
            , error: function(xmlHttpRequest, status, err) {
                alert('Sorry! Error happens.' + err);
            }
 }
        );

Here is the asp.net webservice

[WebMethod()]
    public bool TempCanMakeCall(string reservationNum, string completedReservationNum )
    {

            return true;
    }
+1  A: 

The jquery ajax call looks fine. I think you need to make sure that the path to "CallService.asmx" is correct. The way it is now, I will only work if the file making the jQuery call is in the same virtual directory as the ASMX.

ichiban
A: 

In your error callback function, you could check 'xmlHttpRequest.status' to get the http code returned from the server. This may give you another clue. If ichiban above is correct, it should be a 404.

Lunchy
A: 

You can check the xmlHttpRequest.responseText property. The response text is very probably an html document returned by the server that contains the reason for the error.

If you are using Visual Studio, you can also enable script debugging in Internet Explorer and put the following keyword in your error function: debugger. The browser sees this as a breakpoint and will invoke a debugger (which should be Visual Studio). Now you can check the entire contents of the xmlHttpRequest instance.

For clarity, your error function will then look like this:

function(xmlHttpRequest, status, err)
{
    debugger;
    ...rest of your function...
}
Ronald Wildenberg
+1  A: 

xmlHttpRequest.responseText has always been my goto when dealing with jQuery AJAX errors.

Try making your ASP.NET function static:

[WebMethod()]
public static bool TempCanMakeCall(string reservationNum, string completedReservationNum )
{

        return true;
}

Also note that the returned JSON value is encapsulated in an object named 'd' (ASP.NET specific.) To display your return value upon success, you would need to do this:

success: function(data, status) {
            alert(data.d);
        }
Ben Koehler
All works a treat now!
mark smith