理请求时出现未知错误.服务器返回的状态码为: 500,react-native
我試圖調用一個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});
}
}
總結
以上是生活随笔為你收集整理的理请求时出现未知错误.服务器返回的状态码为: 500,react-native的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 节食减肥不排便怎么办
- 下一篇: ajax中迭代是什么意思,Ajax 局部