【问题标题】:Passing data to Web Service getting - 500 (Internal Server Error)将数据传递给 Web 服务获取 - 500(内部服务器错误)
【发布时间】:2021-10-27 02:56:09
【问题描述】:

我在从 ReactJS 获取数据并将数据传递到 ASP.NET Web 服务时遇到问题。

仅在从数据库中获取数据时,其他获取功能正在工作。 为什么我会收到这些错误?我在这里做错了什么? 我相信它在我的signUp fetch 函数中。

我认为该响应是 fetch 无法读取来自服务器的响应 -

main.chunk.js:2417 POST http://localhost:50496/WebService.asmx/SignUp 500 (Internal Server Error)
SyntaxError: Unexpected token T in JSON at position 0  at JSON.parse (<anonymous>)

ReactJS

userData = {
    fName: 'some',
    lName: 'two one',
    email: 'someonw@gmail.com',
    password: 'se123456'  }

我在 ReactJs 中的 fetch 函数 -

   const signUp = (signUpData) => {
 
    let data = {
      firstName: signUpData.fName,
      lastName: signUpData.lName,
      emailAddress: signUpData.email,
      password: signUpData.password,
    };
    fetch('http://localhost:50496/WebService.asmx/SignUp', {
      body: JSON.stringify(data),
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
      },
    })
      .then((response) => response.text())
      .then(
        (xml) =>
          new window.DOMParser().parseFromString(xml, 'text/xml')
            .documentElement.firstChild.textContent
      )
      .then((jsonStr) => JSON.parse(jsonStr))
      .then((userData) => {
        if (userData.d  && userData.d.length > 0) {
          setUser(userData);
        } else {
          console.log('error no data');
        }
      })
      .catch((err) => {
        console.log(err);
      });
  };

Chrome 出错 -

System.InvalidOperationException:缺少参数:firstName。 System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection 集合) System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest 请求) System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

SignUpWebMethod 正在 WebService 中工作。

[WebMethod]
public string SignUp(string firstName, string lastName, string emailAddress, string password)
{
   return BLL.SignUp(firstName, lastName, emailAddress, password);
}

【问题讨论】:

    标签: asp.net reactjs web-services fetch internal-server-error


    【解决方案1】:

    我找到了我需要做的,
    另一个错误是 -

    InvalidOperationException:仅具有 [ScriptService] 的 Web 服务 类定义中的属性可以从脚本中读取。

    所以我在 WebService.cs 中添加了 Attribute Class[ScriptService]

     [WebService(Namespace = "http://tempuri.org/")]
     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
     [ScriptService]
    

    而且由于这完全消除了在使用标准 SOAP(简单对象访问协议)Web 服务时必须解析 XML 的需要,
    我删除了这些代码行 -

    .then((response) => response.text())
          .then(
            (xml) =>
              new window.DOMParser().parseFromString(xml, 'text/xml')
                 .documentElement.firstChild.textContent 
          )
          .then((jsonStr) => JSON.parse(jsonStr))
    

    然后改成这个-

     .then((response) => response.json())
      .then((userData) => {
        if (userData.d && userData.d.length > 0) {
          let user = JSON.parse(userData.d);
          setUserData(user);
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2014-10-16
      • 2014-07-06
      • 1970-01-01
      • 2012-05-05
      • 2015-07-13
      • 1970-01-01
      相关资源
      最近更新 更多