admin管理员组

文章数量:1279187

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.

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.

Share Improve this question edited Apr 23, 2013 at 7:59 Passerby 10.1k2 gold badges36 silver badges52 bronze badges asked Apr 23, 2013 at 7:13 pavanpavan 4703 gold badges11 silver badges25 bronze badges 7
  • A javascript error on client side can not cause 500, because as the code status indicates, it's an internal server error. There MUST be something wrong on your server side. – Passerby Commented Apr 23, 2013 at 8:01
  • data: "{'sDB': '" + "sDB" + "'}" this line will ends up as data:"{'sDB': 'sDB'}", literally. Is that what you really mean? – Passerby Commented Apr 23, 2013 at 8:05
  • Thanks for the reply, i tried with this and its not working for me. – pavan Commented Apr 23, 2013 at 9:23
  • No, I was asking/informing you, that the data part may not work as you expected, is the string literal really what you want? – Passerby Commented Apr 23, 2013 at 9:26
  • yes. I tried passing the values to the query in the web service and it dint work for me, so i tried static way. – pavan Commented Apr 23, 2013 at 9:39
 |  Show 2 more ments

2 Answers 2

Reset to default 4

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

Changing datatype from json to text fixed my problem

本文标签: