使用fetch进行本机反应时出现res.json()错误

时间:2018-08-07 08:57:19

标签: c# web-services react-native json.net asmx

嗨,我使用访存连接到我的数据库。客户端是React Native,服务器端是C#,数据库是SQL。 我遇到了问题,我尝试了我在这里阅读的所有内容,现在可以使用。

首先我使用res.json();它给我错误 json解析错误无法识别的标记“ <”,我读了它,因为在某些地方我返回HTML文件支持JSON,但是在哪里? 所以我将res.json()切换到res._bodyText,然后看到它返回未定义。

客户端(React Native):

signUP = () => {
alert('start to signup')
let urlAPI = "http://proj.ruppin.ac.il/cegroup2/prod/WebService.asmx/";
fetch(urlAPI + 'SignUp' , {
  method: 'POST',
  body: JSON.stringify(
    {
      firstName: this.state.firstname,
      lastName: this.state.lastname,
      email: this.state.email,
      password: this.state.password,
      repassword: this.state.repassword,
    }),
  headers: {
    "Content-Type": "application/json; charset=UTF-8"
  }
}) 
  .then(res => {console.log(res);return res._bodyText;})
  .then(data => {
    let person = JSON.parse(data.d);
    alert(
      `firstName= ${person.firstName} lastName: ${person.lastName} email: ${person.email} password: ${person.password}`);
  },
  // Note: it's important to handle errors here
  // instead of a catch() block so that we don't swallow
  // exceptions from actual bugs in components.
  (error) => {
    alert("error-" + error);
  });

服务器端(C#):

        [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SignUp(string firstName, string lastName, string email, string password, string repassword)
    {
        Person p = new Person();
        if (password == repassword)
        {
            p.email = email;
            p.firstName = firstName;
            p.lastName = lastName;
            p.password = password;
            string conStr = ConfigurationManager.ConnectionStrings["WebService"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            SqlCommand com = new SqlCommand("INSERT INTO TBUsers (UserEmail,UserFirstName,UserLastName,UserPassword) VALUES (@email,@firstname,@lastname,@password)", con);
            com.Parameters.Add("@email", email);
            com.Parameters.Add("@firstname", firstName);
            com.Parameters.Add("@lastname", lastName);
            com.Parameters.Add("@password", password);
            com.ExecuteNonQuery();
            con.Close();
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        return(js.Serialize(p));
    }

如果我直接使用Web服务,而不是从客户端的工作中使用,并且看到数据库中的用户,但可以从本机反应我可以注册...

谢谢大家。

0 个答案:

没有答案