使用AJAX调用Web服务时出现错误500

问题描述 投票:0回答:1

我在 Visual Studio 中有一个带有一个参数的 Web 服务解决方案:

Public Function CheckPalletInLocation(location As String) As String

       Dim ScaleConnnection As New SqlClient.SqlConnection("MYCONNEXION")
       Dim ScaleCommand As New SqlClient.SqlCommand
       ScaleCommand.Connection = ScaleConnnection
       ScaleConnnection.Open()
       ScaleCommand.CommandText = "SELECT DISTINCT LOGISTICS_UNIT FROM LOCATION_INVENTORY WHERE LOCATION = '" &
       location & "'"
       Return ScaleCommand.ExecuteScalar()
       ScaleConnnection.Close()

       'Return "True"
   End Function

当我启动解决方案时,网络服务就可以工作。它返回值。 现在我想用ajax代码在HTML页面中调用它:

<pre><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
        <title>TEST WEB SERVICE</title>
        <script type="text/javascript" src="jquery-3.4.1.js"></script>
</HEAD>

    <BODY onload="Click_BB();">PAGE
    <script>

    function Click_BB()
    {
        $.ajax({
          type: 'post',
          url: 'https://localhost:44341/WebService1.asmx?op=CheckPalletInLocation',
          contentType: "application/xml",
          data: { location: '110-01-03-10' },
          success: function (d) 
              {
                alert(d);
              },
          failure: function (response) 
              {
                debugger;
                alert(response.d);
              }
        });
    }
    
    </script>
    </BODY>
</HTML>

我在响应页面中总是出现此错误:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="fr">System.Web.Services.Protocols.SoapException: Le serveur n'a pas pu traiter la demande. ---> System.Xml.XmlException: Données non valides au niveau racine. Ligne 1, position 1.

我翻译:服务器无法处理。数据根无效,第 1 行位置 1

有人可以帮忙吗?

谢谢

我尝试更改 AJAX 代码,我希望在 HTML 页面中获得 Web 服务的结果。到现在为止我的错误是500

javascript html asp.net ajax
1个回答
0
投票

您想要将带有正确位置数据的 XML(SOAP 信封)发送到服务器:

添加一个包含有效

soap-envelope
的变量作为 Web 服务使用的字符串。将这些属性添加到
Click_BB
函数中:

dataType: "xml",

data: soapMessage,  

function Click_BB()
{
    $.ajax({
      type: 'post',
      url: 'https://localhost:44341/WebService1.asmx?op=CheckPalletInLocation',
      contentType: "application/xml",
      dataType: "xml", 
      data: soapMessage, 
      success: function (d) 
          {
            alert(d);
          },
      failure: function (response) 
          {
            debugger;
            alert(response.d);
          }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.