" Failed to load resource: the server responded with a status of 500 (Internal Server Error)"

30,252

Did you try to insert the URL you are requesting directly into the browser ... You will receive an error message telling you that ,

"Only Web services with a [ScriptService] attribute on the class definition can be called from script"

add the [ScriptService] attribute to the top of your service class definition, and it may solve your problem.

I had a similar problem and It worked for me :D

Share:
30,252
pavan
Author by

pavan

Updated on January 03, 2020

Comments

  • pavan
    pavan over 4 years

    I really can't understand what is Exact problem here while calling the web service in the html page with JavaScript using ajax as it produces the error below:

    Failed to load resource: the server responded with a status of 500 (Internal Server Error)

    Ajax Code:

    function Image() {
        $.ajax({
    
            type: "POST",
            url: "WebService.asmx/GetImage",
            data: "{'sDB': '" + "sDB" + "'}", 
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnGetMemberSuccess,
            failure: function (errMsg) {
                $('#errorMessage').text(errMsg);  //errorMessage is id of the  div
            }
        });
        function OnGetMemberSuccess(data, status) {
            alert("data" + data.d);
            $("#MemberDetails").html(data.d);
            $('input[type=button]').attr('disabled', false);
        }
    }
    

    Where sDB is Null.

    Button click code:

    <input type="button" id="Button" value="Image" onclick="Image()" />
    

    I have used the same code in my previous projects but its working fine.

    Web service code:

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <WebMethod()> _
    Public Function GetImage()
    
        Dim cmd As SqlCommand = New SqlCommand
        Dim con = New SqlConnection("server = PROG19-PC;database = XIDBViews;Trusted_Connection = yes")
        cmd.Connection = con
        con.Open()
        Dim strQuery As String = ""
        Dim oSB As New StringBuilder
        Dim table As New Table()
        Dim tr As New TableRow()
        Dim td As New TableCell()
        Dim sFirstNameValue As String = String.Empty
        Dim sLastNameValue As String = String.Empty
        Dim DoBValue As String = String.Empty
        Dim sPhoto As String = String.Empty
    
        strQuery = "SELECT [sFirstName],[sLastName],[DoB],[sPhoto] FROM [XIDBViews].[dbo].[tblEmployee]  "
        cmd = New SqlCommand(strQuery, con)
        cmd.ExecuteNonQuery()
        Dim dr As SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        oSB.Append("<table><thead><tr><th>" + " FirstName" + "</th><th>" + "Lastname" + "</th><th>" + "DoB" + "</th><th>" + "Party" + "</th><th>" + "Photo" + "</th></tr></thead>")
        While dr.Read()
            sFirstNameValue = dr("sFirstName").ToString
            sLastNameValue = dr("sLastName").ToString
            DoBValue = dr("DoB").ToString
            sPhoto = dr("sPhoto").ToString
    
            oSB.Append("<tbody id=tbodyid'>")
    
            oSB.Append("<tr>")
            oSB.Append("<td class=border1>")
            oSB.Append(sFirstNameValue)
            oSB.Append("</td>")
    
            oSB.Append("<td class=border1 >")
            oSB.Append(sLastNameValue)
            oSB.Append("</td>")
    
            oSB.Append("<td  class=border1>")
            oSB.Append(DoBValue)
            oSB.Append("</td>")
    
            oSB.Append("<td class=border1>")
            oSB.Append(sPhoto)
            oSB.Append("</td>")
    
            oSB.Append("</tr>")
            oSB.Append("</tbody>")
        End While
        dr.Close()
        con.Close()
        MsgBox(oSB.ToString)
        'Debug.Print(oSB.ToString)
        Return oSB.ToString()
    End Function
    End Class
    

    But this web service code is working fine and according to my knowledge problem is with the ajax code, can anyone please help me with this. Cheers.