在React JS中使用Web Api

时间:2019-07-01 13:50:30

标签: ios reactjs asp.net-web-api

我正在尝试在React Js中使用Web Api,但它没有与之连接,但是此Api在Android Project上工作正常,您能帮我解决问题的地方或更好的方法吗,我不知道关于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,
}
);
});
}
}

0 个答案:

没有答案