我试图调用一个api,但出现错误“未处理的承诺被拒绝:错误:请求失败,状态码为500”。 我不知道如何得到错误。我将api调用放在componentDidMount中。 我不确定错误是来自redux实现还是来自我的api。
这就是我所谓的api。 成功登录后,我将用户名作为令牌,并使用它来调用另一个api。
state={
username: '',
semcode: [
{}
]
}
componentWillMount() {
AsyncStorage.getItem('Login_token').then((token) => {
console.log('this is coursescreen',token);
let Login_token = token;
this.setState({ username: Login_token });
});
}
componentDidMount(){
this.props.getSemcode(this.state.username);
}
componentWillReceiveProps(nextProps) {
console.log('xx',nextProps);
if (nextProps.semCode != undefined) {
this.setState({ semcode: nextProps.semCode });
}
}
这是我编写动作文件的方式:
export const getSemcode = (username) => async dispatch => {
let param = {
nomatrik: username,
}
console.log(`${helper.ROOT_URL}/result/GetListOfKodSesiSem`)
let code_res = await
axios.post(`${helper.ROOT_URL}/result/GetListOfKodSesiSem`, param)
console.log(code_res.data);
if (code_res.data.length > 0) {
const { code } = code_res.data;
dispatch({ type: SEMCODE_FETCH_SUCCESS, payload: { semCode: code }});
}
}
这是我编写减速器的方式:
import { SEMCODE_FETCH_SUCCESS} from '../actions/types';
const INITIAL_STATE={
semCode:[],
}
export default function (state=INITIAL_STATE, action){
switch(action.type){
case SEMCODE_FETCH_SUCCESS:
return action.payload
default:
return state;
}
}
谁能帮助我
错误信息
从axios.post接收到错误:{“ config”:{“ transformRequest”:{},“ transformResponse”:{},“超时”:0,“ xsrfCookieName”:“ XSRF-令牌”,“ xsrfHeaderName”:“ X- XSRF-TOKEN“,” maxContentLength“:-1,” headers“:{” Accept“:” application / json,text / plain, / “,” Content- Type“:” application / json; charset = utf-8“ },“ method”:“ post”,“ nomatrik”:“ BB16160907”,“ url”:“ https://smp.ums.edu.my/api/result/GetListOfKodSesiSem ”,“ data”:“ {\\”接受\\“:\\”应用程序/ json \\“,\\”内容类型\\“:\\”应用程序/ json \\“}”},“请求”:{“ UNSENT”:0,“ OPENED”:1,“ HEADERS_RECEIVED “:2,” LOADING“:3,” DONE“:4,” readyState“:4,”状态“:500,”超时“:0:” withCredentials“:true,”上传“:{},” _ aborted“ :false,“ _ hasError”:false,“ _ method”:“ POST”,“ _ response”:“ {\\” Message \\“:\\”发生错误。\\“}”,“ _url”:“ https:// smp.ums.edu.my/api/result/GetListOfKodSesiSem “,” _timedOut“:false,” _ trackingName“:”未知“,” _incrementalEvents“:false,” responseHeaders“:{” Date“:” 2017年12月30日,星期六03:58:25 GMT“,”内容长度“:” 36“,” X-Powered-By“:” ARR / 3.0“,” X-AspNet-Ve rsion“:” 4.0.30319“,” Expires“:”-1“,” Content-Type“:” application / json; charset = utf-8”,“服务器”:“ Microsoft-IIS / 10.0”,“编译指示”:“ no-cache”,“ Cache-Control”:“ no-cache”},“ _ requestId”:null,“ _ headers” “:{” accept“:”应用程序/ json,文本/纯文本, / “,”内容类型“:”应用程序/ json; charset = utf-8“},” _ responseType“:”“,” _ sent“:true ,“ _ lowerCaseResponseHeaders”:{“ date”:“星期六,2017年12月30日03:58:25 GMT”,“ content-length”:“ 36”,“ x-powered by”:“ ARR / 3.0”,“ x -aspnet-version“:” 4.0.30319“,” expires“:”-1“,” content-type“:” application / json; charset = utf-8“,”服务器“:” Microsoft-IIS / 10.0“,” pragma“:”无缓存“,”缓存控制“:”无缓存“},” _ subscriptions“:[],” responseURL“:” https://smp.ums.edu.my/api/result/GetListOfKodSesiSem “},” response“:{” data“:{” Message“:”发生错误。“},”状态“ :500,“ headers”:{“ date”:“ Sat,30 Dec 2017 GMT”,“ content-length”:“ 36”,“ x-powered by”:“ ARR / 3.0”, “ x- aspnet-version”:“ 4.0.30319”,“ expires”:“-1”,“ content-type”:“ application / json; charset = utf-8“,”服务器“:” Microsoft-IIS / 10.0“,” pragma“:” no-cache“,” cache-control“:” no-cache“},” config“:{” transformRequest“ :{},“ transformResponse”:{},“超时”:0,“ xsrfCookieName”:“ XSRF-TOKEN”,“ xsrfHeaderName”:“ X-XSRF-TOKEN”,“ maxContentLength”:-1,“标题”: {“ Accept”:“应用程序/ json,文本/纯文本, / ”,“ Content-Type”:“ application / json; charset = utf-8”},“ method”:“ post”,“ nomatrik”:“ BB16160907 “,” url“:” https://smp.ums.edu.my/api/result/GetListOfKodSesiSem “,” data“:” {\\“ Accept \\”:\\“ application / json \\”,\\“ Content- Type \\“:\\” application / json \\“}”},“ request”:{“ UNSENT”:0,“ OPENED”:1,“ HEADERS_RECEIVED”:2,“ LOADING”:3,“ DONE”:4, “ readyState”:4,“ status”:500,“ timeout”:0:“ withCredentials”:true,“ upload”:{},“ _ aborted”:false,“ _ hasError”:false,“ _ method”:“ POST” ,“ _ response”:“ {\\”消息\\“:\\”发生错误。\\“}”,“ _url”:“ https://smp.ums.edu.my/api/result/GetListOfKodSesiSem ”,“ _timedOut“:false,” _ trackingName“:”未知“,” _ incrementalEvents“:false,” responseHeaders“:{” Date“:” Sat,30 Dec 2017 03:58:25 GMT“,” Content- Length“:” 36“,” X-Powered-By“:” ARR / 3.0“,” X-AspNet- Version“:” 4.0.30319“,” Expires“:”-1“,” Content-Type “:” application / json; charset = utf-8”,“服务器”:“ Microsoft-IIS / 10.0”,“编译指示”:“ no-cache”,“ Cache-Control”:“ no-cache”},“ _ requestId”:null,“ _ headers” “:{” accept“:”应用程序/ json,文本/纯文本, / “,”内容类型“:”应用程序/ json; charset = utf-8“},” _ responseType“:”“,” _ sent“:true ,“ _ lowerCaseResponseHeaders”:{“ date”:“星期六,2017年12月30日03:58:25 GMT”,“ content-length”:“ 36”,“ x-powered by”:“ ARR / 3.0”,“ x -aspnet-version“:” 4.0.30319“,” expires“:”-1“,” content-type“:” application / json; charset = utf-8“,”服务器“:” Microsoft-IIS / 10.0“,” pragma“:”无缓存“,”缓存控制“:”无缓存“},” _ subscriptions“:[],” responseURL“:” https://smp.ums.edu.my/api/result/GetListOfKodSesiSem “}}}
登录操作:
export const attemptLogin = (username, password) => async dispatch => {
let param = {
txtNomatrik: username,
txtPwd: password,
public_key: helper.PUBLIC_KEY,
secret_key: helper.SECRET_KEY
}
console.log(`${helper.ROOT_API_URL}/v1/basic/ad/std/login`)
let login_res = await
axios.post(`${helper.ROOT_API_URL}/v1/basic/ad/std/login`, param)
console.log(login_res.data);
await AsyncStorage.setItem('jwtToken',login_res.data.token);
if (login_res.data.status == 'Successful login') {
const { login } = login_res.data;
dispatch({ type: LOGIN_SUCCESS});
}
}