【发布时间】:2016-12-19 06:43:57
【问题描述】:
我正在使用 slim api 集成在 angular js 中开发登录表单。我在此项目中使用了 slim 中的多个 api,但仅当我尝试使用 post 请求登录时才显示错误。当我使用 post 请求向服务器发送登录请求时,它会在控制台上抛出错误。
Failed to load resource: the server responded with a status of 404 (Not Found)
XMLHttpRequest cannot load http://example.com/Youin-api/v1/business/login. Response for preflight has invalid HTTP status code 404
我的后端代码:
$app->post('/business/login', function() use($app) {
$postvar = json_decode($app->request->getBody(), true);
verifyRequiredParams(array('emailid', 'pwd'));
$email = $postvar['emailid'];
$pwd = $postvar['pwd'];
// validating email address
validateEmail($email);
$db = new DbHandler();
$response = $db->loginBuser($email, $pwd);
// echo json response
echoRespnse(200, $response);
});
控制器:
public function loginBuser($email, $pwd)
{
$response = array();
$stmt = $this->conn->prepare("SELECT BID from business_login_tbl WHERE emailid = ? and login_pwd = ?";
$stmt->bind_param("ss",$email,$pwd);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
$stmt->close();
if($num_rows > 0)
{
$response["code"] = "Success";
$response["error"] = false;
$response["user"] = $this->getBusinessuserByEmail($email);
}
else
{
$response["code"] = "failed";
$response["error"] = true;
$response["message"] = "Oops! Username of password Missmatch";
}
return $response;
}
我的回声功能:
function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
我也在使用 cors:
$app = new \Slim\Slim();
$response = $app->response();
$response>header('Access-Control-Allow-Origin', 'http://example.com');
$response->header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, X-authentication, X-client');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
我不知道我的前端和后端代码有什么问题。提前谢谢..!
【问题讨论】:
-
不,它不是重复的。我使用的是 slim 框架,而且错误仅在我尝试登录时显示。我在同一个项目中使用 slim 中的多个 url。