APIを呼び出そうとしていますが、「未処理の約束拒否:エラー:要求がステータスコード500で失敗しました。私はどのように私はエラーを取得したかわからない。私はcomponentDidMountでAPIコールを入れた。私はエラーがreduxの実装から来るのか、私がどのようにAPIを呼び出したのかはわかりません。未処理約束拒否:エラー:ステータスコード500で要求が失敗しました
これは私が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 });
}
}
これは私が私のアクションファイルに書いた方法です:
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;
}
}
誰も私が
エラーメッセージをpleaseeeeee助けることができます:これは私が私の減速を書いた方法です
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 }});
}
}
を
Error received from axios.post: {"config":{"transformRequest": {},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF- TOKEN","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":" {\"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":" {\"Message\":\"An error has occurred.\"}", "_url":" https://smp.ums.edu.my/api/result/GetListOfKodSesiSem ", "_timedOut":false,"_trackingName":"unknown", "_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","Server":"Microsoft-IIS/10.0","Pragma":"no-cache","Cache- Control":"no-cache"},"_requestId":null,"_headers": {"accept":"application/json, text/plain, /","content- type":"application/json;charset=utf- 8"},"_responseType":"","_sent":true,"_lowerCaseResponseHeaders":{"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","server":"Microsoft-IIS/10.0","pragma":"no-cache","cache- control":"no-cache"},"_subscriptions":[],"responseURL": " https://smp.ums.edu.my/api/result/GetListOfKodSesiSem "},"response":{"data": {"Message":"An error has occurred."},"status":500,"headers":{"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","server":"Microsoft-IIS/10.0","pragma":"no-cache","cache- control":"no-cache"},"config":{"transformRequest":{},"transformResponse": {},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","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":" {\"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":" {\"Message\":\"An error has occurred.\"}", "_url":" https://smp.ums.edu.my/api/result/GetListOfKodSesiSem ", "_timedOut":false,"_trackingName":"unknown","_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","Server":"Microsoft-IIS/10.0","Pragma":"no-cache","Cache- Control":"no-cache"},"_requestId":null,"_headers": {"accept":"application/json, text/plain, /","content- type":"application/json;charset=utf- 8"},"_responseType":"","_sent":true,"_lowerCaseResponseHeaders":{"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","server":"Microsoft-IIS/10.0","pragma":"no-cache","cache- control":"no-cache"},"_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});
}
}
郵便配達員をダウンロードすることをお勧めします。すばらしいGUIでAPI呼び出しをテストできる、本当にきれいなアプリです。それはあなたに応答を示し、あなたのアプリケーションをデバッグするのに役立ちます。 https://www.getpostman.com/幸運を祈る! – wuno
apiは郵便配達員で問題なく動作しています。私はエラーをデバッグしようとしましたが、私はそれを解決する方法を知らない以下のようなエラーが発生しました – gdphy
クール私はあなたを助けてくれるでしょう。いくつか教えてください。 1.どのAPIを使用していますか? 2. apiエンドポイントに必要なトークンを確実に渡していますか? 3.私に見せることができるAPIのドキュメントはありますか?変数usernameにトークンを渡し、それをパラメータとして設定するようです。その後、AxiosのPOST要求を行います。私はこれまでにこのような問題を抱えていました。それはAPIの100%があなたのクエリを特定の方法でフォーマットすることを期待しており、私はそれを少し間違ってやっていました。 Content-Type:application/jsonヘッダーを設定するだけで簡単にできます。試して情報を私に送ってください。 – wuno