Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从函数中调用类_Javascript_Reactjs_Axios_Interceptor - Fatal编程技术网

Javascript 如何从函数中调用类

Javascript 如何从函数中调用类,javascript,reactjs,axios,interceptor,Javascript,Reactjs,Axios,Interceptor,基本上,我试图从axis响应调用一个模型类。我想显示一个模式弹出窗口,而不是下面的警告代码。有人能帮助我们如何从Axios拦截器函数调用modal类吗?提前谢谢 import React, { Component} from 'react'; import axios from 'axios'; import Modals from '../components/modalAlerts/modalalerts' const instance = axios.create({ base

基本上,我试图从axis响应调用一个模型类。我想显示一个模式弹出窗口,而不是下面的警告代码。有人能帮助我们如何从Axios拦截器函数调用modal类吗?提前谢谢

import React, { Component}  from 'react';
import axios from 'axios';
import Modals  from '../components/modalAlerts/modalalerts'
const instance = axios.create({
    baseURL: 'http://someurl',
    timeout: 15000,
}); 

instance.defaults.headers.common['Authorization'] = //sequrity token will be here
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

instance.interceptors.request.use(function (config) {

    return config;
  }, function (error) {

    alert(error)
    return Promise.reject(error);
  });
instance.interceptors.response.use(function (config) {

    return config;
  }, function (error) {
    console.log(error)
    if(error.response){
        if(error.response.status === 401||error.response.status === 403 ){
            localStorage.clear()
            alert(error.response.data.message)
        }else if(error.response.status === 404){
            console.log("404")
        }else if(error.response.status === 400){
           alert(error.response.data.message)
        }else{
            alert("something went wrong. Please try after sometime..!")
        }
    }else{
        alert("server not found")
    }

    return Promise.reject(error);
  });
export default instance;

做下面这样的事情

您需要为此设置一个布尔标志。使用
this.state在构造函数中初始化布尔标志

  constructor(props){
      super(props){
          this.state = {
              callModal: false,
              errorMessage: ""
          }
      }
  }
在下面的代码中,您需要将常规功能更改为箭头功能,以便访问
内部

 axios.interceptors.response.use(response => {      
    return response;
  }, error => {
    // set callModal flag to true using this.setState
    this.setState({
        callModal: true,
        errorMessage: error
    });
    return Promise.reject(error);
  });
或者绑定该函数,以便可以访问此

   axios.interceptors.response.use(function (response) {      
    return response;
  }.bind(this), function (error) {
    // set callModal flag to true using this.setState
    this.setState({
        callModal: true,
        errorMessage: error
    });
    return Promise.reject(error);
  }.bind(this));
现在在呈现调用模式类中

     render(){
         const { callModal, errorMessage } = this.state;
         return(){
            <div>
                {callModal && <ModalComponent errorMessage={errorMessage} />
            </div>
         }
     }
render(){
const{callModal,errorMessage}=this.state;
返回(){
{callModal&&
}
}

要使相同的功能第二次工作,当用户使用callback单击modal中的cancel按钮时,您需要将callModal设置为false

这是modal popup,但不是model popup。OP似乎错误地使用了model而不是modal。此解决方案将以他的方式工作。我在手机中应答,因此我的手机中没有正确的双引号移动键盘:)不起作用@think Two Two,但感谢您的响应。您没有提供太多代码,我给了您一个详细的提示,让事情顺利进行。我的回答将帮助您实现您想要的。您需要了解我在代码中解释和实现的步骤,这些步骤您尚未共享。如果不起作用,这意味着您需要我只是更新了我的代码,你可以检查一次@三思而后行