Home > Net >  ASP.NET VB WebService request with AJAX 500 error
ASP.NET VB WebService request with AJAX 500 error

Time:12-28

I'm trying to run an AJAX Webservice request on a VB ASP.NET page.

When the page loads, I'm trying to call the webservice but I get a 500 error in the console.

My WebService file looks like this:

<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 usrDataSave
    Inherits System.Web.Services.WebService
    <WebMethod()>
    Public Function saydata(abc As String)
        MsgBox(abc)
        Return abc

    End Function

My ASP.NET page looks like this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script> 
    <script type="text/javascript">  
    $(document).ready(function() {  
        $.ajax({  
            type: "POST",  
            url: "usrDataSave.asmx/saydata",
            data: "hello_world",  
            contentType: "application/json",  
            datatype: "json",  
            success: function(responseFromServer) {  
                alert(responseFromServer.d)  
            }  
        });  
    });  
    </script> 

    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

I expect the page to load and a message box to popup server side that says 'hello_world' as well as the web browser to create a popup that says the same. However, this does not happen as I get a 500 error instead.

I've tried to fix this by using different versions of jQuery as well as enabling requests in the web.config file like this:

 <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>

This doesn't work and I still get that "the server responded with a status of 500" in the web browser console. No errors are logged within the application's debug console.

How can I fix this?

CodePudding user response:

Ok, assuming both pages are in the SAME folder - at the same level?

Then this should work:

   <script type="text/javascript">  
       $(document).ready(function () {
           $.ajax({
               type: "POST",
               url: usrDataSave.asmx/saydata
               data: "{abc: 'hello_world'}",
               contentType: "application/json",
               datatype: "json",
               success: function (responseFromServer) {
                   alert(responseFromServer.d)
               }
           });
       });
   </script> 

Note how your data has to match your parmaters..

So, say you have this:

<WebMethod()>
Public Function saydata(abc As String, def as string) as string
    MsgBox(abc)
    Return abc & " " & def

End Function

And note how we set the function as string - you should give the function a type - in this case "string".

   <script type="text/javascript">  
       $(document).ready(function () {
           $.ajax({
               type: "POST",
               url: "WebService1.asmx/saydata",
               data: "{abc: 'hello', def: 'world'}",
               contentType: "application/json",
               datatype: "json",
               success: function (responseFromServer) {
                   alert(responseFromServer.d)
               }
           });
       });
   </script> 
  • Related