2017-04-10 59 views
0

我正在使用構建在開發服務器中的webpack構建一個響應站點。我使用webpack-dev-server --content-base src --inline --hot運行開發服務器。它部署在localhost:8080。 我也使用php和mySQL在localhost:80上運行Apache服務器。 我正在向使用axios的Apache服務器發出請求,這裏是腳本。axios爲本地apache-php請求引發錯誤

import dispatcher from "../dispatcher"; 
import axios from "axios"; 
let handlersURL = "http://localhost:80/missionariesvote/phpscripts/" 
export function sendUserInfo(name, email, phone) { 
    axios.post(handlersURL + "senduserinfo.php", 
    { 
     name, 
     email, 
     phone 
    }) 
    .then((response) => { 
     dispatcher.dispatch({type: "FETCH_TWEETS_FULFILLED", payload: response.data}) 
    }) 
    .catch((err) => { 
     dispatcher.dispatch({type: "FETCH_TWEETS_REJECTED", payload: err}) 
    }) 
} 

的senduserinfo.php腳本

<?php 
    header('Access-Control-Allow-Origin: *'); 
    $name = filter_input(INPUT_POST, 'name'); 
    $phoneNumber = filter_input(INPUT_POST, 'phone'); 
    $email = filter_input(INPUT_POST, 'email'); 
    if (
      !empty($name) && 
      !empty($phoneNumber) && 
      !empty($email) && 
      filter_var($email, FILTER_VALIDATE_EMAIL) 
    ) { 
     require_once './conectvars.php'; 
     $userIdSQL = "SELECT iduserinfo FROM userinfo WHERE `name` = ?"; 
     $userIdStmt = $link->prepare($userIdSQL); 
     $userIdStmt->bind_param("s", $name); 
     $userIdStmt->execute(); 
     $userIdStmt->bind_result($idUser); 
     $userIdStmt->fetch(); 
     $userIdStmt->close(); 
     if(empty($idUser)){ 
      $addUserSQL = "INSERT INTO iduserinfo (`name`,`phonenumber`, `email`) 
      VALUES (?, ?, ?)"; 
      $addUserStmt = $link->prepare($addUserSQL); 
      $addUserStmt->bind_param("sss", $name,$phoneNumber, $email); 
      $addUserStmt->execute(); 
      $addUserStmt->close(); 

      $userIdSQL = "SELECT iduserinfo FROM userinfo WHERE `name` = ?"; 
      $userIdStmt = $link->prepare($userIdSQL); 
      $userIdStmt->bind_param("s", $name); 
      $userIdStmt->execute(); 
      $userIdStmt->bind_result($idUser); 
      $userIdStmt->fetch(); 
      $userIdStmt->close(); 
     } 
     $votesUserIdSQL = "SELECT iduser FROM votes WHERE `iduser` = ?"; 
     $votesUserIdStmt = $link->prepare($votesUserIdSQL); 
     $votesUserIdStmt->bind_param("s", $idUser); 
     $votesUserIdStmt->execute(); 
     $votesUserIdStmt->bind_result($voteUserId); 
     $votesUserIdStmt->fetch(); 
     $votesUserIdStmt->close(); 
     if($voteUserId){ 
      echo'1'; 
     } 
     } 
     else{ 
     echo'1'; 
     } 
    $link->close(); 
    } 

我得到這個錯誤在命令提示符下

 XMLHttpRequest cannot load http://localhost/missionariesvote/phpscripts/senduserinfo.php. Response to 
    preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access 

我很難過。 github項目在這裏 https://github.com/theRealFakeRock/missionariesVote 您可以通過從根文件夾運行npm run dev來運行dev服務器。 謝謝

+0

檢查此:http://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-https-www-website-com –

回答

0

在您的senduserinfo.php第一行添加以下代碼。

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT'); 
header('Access-Control-Allow-Headers: Host, Connection, Accept, Authorization, Content-Type, X-Requested-With, User-Agent, Referer, Methods'); 
if($_SERVER["REQUEST_METHOD"]== "OPTIONS"){ 
    echo "";die; 
}