Make jQuery Ajax Call on the main page?

Am I missing something? I am trying to make a simple ajax call using jquery for a web service on my site, and every time I make a call from the main page, I get a 500 error. No one has ever made an ajax call from the main page, or I'm just crazy and extremely sleep deprived?

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

Is something wrong? Do I have a misunderstanding of the main page? Please, help!

+3
source share
1 answer

, , . , . , , , , :

1) , , , ajax jQuery , - , . , , :

$.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) jQuery ajax, -. , - script :

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<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

, , . , , ...

+4

Source: https://habr.com/ru/post/1763932/


All Articles