ajax “加载资源失败:服务器响应状态为 500(内部服务器错误)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16163156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:00:12  来源:igfitidea点击:

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

javascriptajaxvb.netweb-services

提问by pavan

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:

在使用 ajax 使用 JavaScript 调用 html 页面中的 Web 服务时,我真的无法理解这里的确切问题是什么,因为它会产生以下错误:

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

加载资源失败:服务器响应状态为 500(内部服务器错误)

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.

其中 sDB 为 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.

但是这个 Web 服务代码运行良好,根据我的知识问题出在 ajax 代码上,任何人都可以帮我解决这个问题。干杯。

回答by Borhan A.Otour

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

您是否尝试将您请求的 URL 直接插入浏览器...您将收到一条错误消息,告诉您,

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

“只有在类定义上具有 [ScriptService] 属性的 Web 服务才能从脚本中调用”

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

将 [ScriptService] 属性添加到您的服务类定义的顶部,它可能会解决您的问题。

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

我有一个类似的问题,它对我有用:D

回答by Minhaj

Changing datatype from json to text fixed my problem

将数据类型从 json 更改为 text 解决了我的问题