【问题标题】:Post request with AngularJS failed with Response for preflight has invalid HTTP status code 404 in Slim Framework使用 AngularJS 的发布请求失败,预检响应在 Slim 框架中具有无效的 HTTP 状态代码 404
【发布时间】: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');

我不知道我的前端和后端代码有什么问题。提前谢谢..!

【问题讨论】:

标签: angularjs slim


【解决方案1】:

您遇到的问题是 CORS。

跨域资源共享。

我最近发布了一些关于如何启用它的文档。

http://www.slimframework.com/docs/cookbook/enable-cors.html

如果您有任何问题或遇到任何问题,请查看并告诉我。

【讨论】:

    【解决方案2】:

    当您通过 $http 服务使用 Content-Type application/json 和自定义标头 (X-...) 发出请求时,浏览器会执行预检请求以检查 CORS 标头。预检请求只是对指定端点的HTTP OPTIONS 请求。如果您的 API 未实现 OPTIONS 处理,您可能会收到 404 错误并且无法处理请求。

    【讨论】:

    • 好的,我接受它。我怎样才能在纤薄的框架上实现它..?
    • 我启用它但仍然没有清除错误。$app = new \Slim\Slim(); $response = $app->response(); $response>header('Access-Control-Allow-Origin', '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 不熟悉,但是应该这样做:$app->options('<login-url>', function () { // Your logic here.. });
    【解决方案3】:

    您的 API 似乎没有您正在调用的端点,或者您的实现出现导致 404 的错误(可能用户不存在)。你打电话给哪个网址?你的后端有这样的东西吗?

    $app->post('<login-url>', function () {
        // Your logic here..
    });
    

    如果您提供更多信息,我们可以进行调查并尽力帮助您。

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 2017-09-26
      • 2018-03-12
      • 2018-10-19
      • 2017-01-28
      • 2018-04-25
      • 2016-10-10
      • 2016-01-22
      相关资源
      最近更新 更多