首页 > 解决方案 > 在 react js 中使用 Web Api

问题描述

我正在尝试在 React Js 中使用 Web Api,它没有与它连接,但是这个 Api 在 Android 项目上运行良好,你能帮我解决问题或更好的方法吗,我不知道 react js ..这是我正在工作的代码..它基本上是用于注册用户

获取 API 的代码

 const createAccount = function(
    email,
    password,
    title,
    firstname,
    lastname,
    middlename,
    mobileno,
    businessname,
    abn,
    dob
) {
    //return fetch(`${Constants.baseURL}api/Thetaxfactor/Post`, {
        return fetch('http://webservice.thetaxfactorapp.com.au/api/TheTaxfactor/Post', {
        method: 'Get',
        headers: {
           'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            //'Accept': 'application/json',
           // 'Content-Type': 'application/json',
        },
        body: HttpUtil.toEncodedForm({
            email,
            password,
            title,
            firstname,
            lastname,
            middlename,
            mobileno: .isUndefined(mobileno) || .isNull(mobileno) || _.isNil(mobileno) || mobileno === "null" ? "" : mobileno,
            businessname,
            abn,
            dob,
        }),
    });
};

onPress 代码

onPress() {
// call getValue() to get the values of the form
const value = this.refs.form.getValue();
if (value) {
const title = value.title;
const firstname = value.firstName;
const middlename = _.isNull(value.middleName) ? '' : value.middleName;
const lastname = value.surname;
const businessname = _.isNull(value.businessName) ? '' : value.businessName;
const password = value.password;
const dob = moment(value.dateOfBirth).format(Constants.API_DATE_FORMAT);
const email = value.emailAddress;
const mobilenumber = value.mobileNumber;
const businessabn = _.isNull(value.businessABN) ? '' : value.businessABN;
this.setState({ loading: true });

Auth.createAccount(
email,
password,
title,
firstname,
lastname,
middlename,
mobilenumber,
businessname,
businessabn,
dob
)
.then(response => {
this.setState({ loading: false });
setTimeout(() => {
if (response.status !== 201) {
// could not login

Alert.alert(
'Cannot Create Account',
'Email address may already be in use, or password is too short (8 characters minimum)',
[
{
text: 'RETRY',
},
],
{
cancelable: true,
}
);
} else {
Alert.alert('Account Created', 'Log in to continue to the main menu');
this.props.navigation.goBack();
}
}, 250);
})
.catch(error => {
this.setState({ loading: false });
Alert.alert(
'Cannot Create Account',
'No internet connection available',
[
{
// 'No internet connection available', [{
text: 'OK',
},
],
{
cancelable: true,
}
);
});
}
}

标签: iosreactjsasp.net-web-api

解决方案


推荐阅读