views:

139

answers:

1

Am I missing something? I'm trying to do a simple ajax call using jquery to a web service on my site and I get a 500 error every time when I make the call from the master page. Has no one ever made an ajax call from a master page or am I just crazy and extremely deprived of sleep?

Example:

MasterPage.master

<script type="text/javascript">
    $(document).ready(function () {
        $.ajaxSetup({ dataType: "json", contentType: "application/json; charset=utf-8" });

        $.ajax({
            url: '<%= ResolveUrl("~/Services/Test.asmx/HelloWorld") %>',
            success: function (data) {
                alert(data);
            },
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
        });

    });
</script>

/Services/Test.asmx

<WebMethod()> _
Public Function HelloWorld() As String
   Return "Hello World"
End Function

See anything wrong? Do I have a misunderstanding of the Master Page? Please help!

A: 

Ok, I believe I have figured out the problem. Now I'm beginning to think I am just sleepy. A couple of issues that I had and I'll list them for everyone else to make sure they DON'T do in the future:

1) I remember reading another post previously that explained that an ajax call via the jQuery library does not like a null object for data so something has to be listed even if it's an empty array. So, that's exactly what I added:

$.ajax({
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            type: 'POST',
            data: [],
            url: '<%= ResolveUrl("~/Test.asmx/HelloWorld") %>',
            success: function (data) {
                alert(data);
            },
            error: function (xhr, err) {
                //alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                //alert("responseText: " + xhr.responseText);
                $('#ajaxResponse').html(xhr.responseText);
            }
        });

2) Once I got past the jQuery ajax problem, I was then presented an error message from the web service itself. Warned me that to be able to call the web service from a script, I have to add the following line:

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")&gt; _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Test
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "[{""Hello World""}]"
    End Function

End Class

This solved it and now I can call it wherever it resides. Thanks for the posts but looks like I was just overlooking things as usual...

Keith