用Angular2 http捕获api 404响应

时间:2016-02-20 05:23:18

标签: http http-status-code-404 response angular

我有api,如果失败则返回400或404错误。

当我运行http.get方法时,它不会捕获订阅上的错误并抛出错误。

Http.get(`api/path/here`).map(res => res.json()).subscribe(
            data => console.log(data),
            error => console.log(error)
        );

以下是我得到的错误

enter image description here

2 个答案:

答案 0 :(得分:11)

您也可以使用http.get(`api/path/here`) .map(res => res.json()) .catch( err => { // handle errors }) .subscribe( data => console.log(data) ); 运算符

Type classtype=Assembly.GetAssembly(typeof(BaseData)).GetType("DerivedData");
BaseData baseobject=  Activator.CreateInstance(classtype) as BaseData;

答案 1 :(得分:7)

<?php

    $objConnect = mysql_connect("localhost","username","password");
    $objDB = mysql_select_db("database");

    /*** for sample */
    // $_POST["username"] = "sun";
    // $_POST["password"] = "live";

    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    $strSQL = "select * from test_users where username = '".$username."' and  password = '".$password."' ";

    $objQuery = mysql_query($strSQL);
    $intNumRows = mysql_num_rows($objQuery);

    if($intNumRows==0)
    {
    echo "Not Done" ;
    }
    else
    {
    echo "Done" ;
    }

    mysql_close($objConnect);
?>

尝试这种方式。