Angular2 http.post gets executed twice

后端 未结 3 2187
旧巷少年郎
旧巷少年郎 2020-12-03 02:26

I came across a weird issue where the Angular2\'s (RC1) Http service executes the http.post call twice. I\'ve debugged my app and I know for a fact this is not a click event

3条回答
  •  借酒劲吻你
    2020-12-03 03:01

    its happening because HTTP OPTIONS executed first, and you have to restrict unwanted HTTP method before executing your Logic, always use isset method,see example below
    
     if(isset($_POST))
     {
        $name = $_POST["name"];
        $country = $_POST["country"];
    
        $sql = 'INSERT INTO user values("' . $name . '","' . $country . '")';
    
                if ( $conn->query($sql)=== TRUE) 
                {
                    $outp =  "Inserted " .  $name . "  and  "  . $country;
                    echo json_encode($outp);
                } else {
                    echo json_encode("Error: " . $sql . "
    " . $conn->error); } } here it will insert row in table only when its POST METHOD.

提交回复
热议问题