错误:XMLHttpRequest.handleError (xhr.js:84) 处的 createError (createError.js:16) 处的网络错误

时间:2020-12-25 09:47:11

标签: reactjs django axios

我正在尝试使用 axios 从 api 获取数据,但出现此错误。我正在使用 django api。我的 api 似乎在邮递员上工作正常,但反应它给了我错误:

getStudentById(id) {
        const url = `${API_URL}/api/student/student-detail/${id}`;
        return axios.get(url);
    }

componentDidMount() {
     let urlId = this.props.match.params.id;
     
    let thisComponent = this;
    studentService.getStudentById(urlId)
      .then(function (response)  {
        console.log(response.data.Name)
         thisComponent.setState({
            id: response.data.id, 
            name: response.data.Name,
            enrollment_no: response.data.Enrollment_No,
            registration_no:response.data.Registration_No,
            semester: response.data.Semester,
            year: response.data.Year,
            course_nam: response.data.Course_Name,
            course_code: response.data.Course_Code,
            images: response.data.images
           });
          
      }).catch(error=>console.log(error))
     
  }

error screenshot

1 个答案:

答案 0 :(得分:0)

这些错误可能是由于以下原因引起的,请确保遵循以下步骤。将本地主机与本地虚拟机(主机)连接起来。在这里,我将 http://localhost:3001/ 连接到 http://abc.test 要遵循的步骤:

  1. 我们必须允许 CORS,在请求头中放置 Access-Control-Allow-Origin: 可能不起作用。安装启用 CORS 请求的 google 扩展程序。*
  2. 确保您在请求中提供的凭据有效。
  3. 确保已配置流浪者。尝试 vagrant up --provision 这使得 本地主机连接到宅基地的数据库。
  4. 尝试更改标题的内容类型。 header:{ 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8;application/json' } 这一点非常重要。