troubleshooting Question

angular 2 post to a database works on firefox but not on chrome

Avatar of derrida
derrida asked on
AngularJavaScriptWeb BrowsersAJAX
16 Comments2 Solutions168 ViewsLast Modified:
so this drive me insane :)
the same code work on firefox but not on chrome.

i test angular 2 "ajax calls", this time sending data to insert into the database, and send back the inserted item.

so this is in the service:
    var theparams = JSON.stringify({
      name: "fabian",
      address: "dizingoff 99, NY",
      lat: 33.060143,
      lng: 39.770557
    var params = 'user=' + theparams;
    var headers = new Headers();
    headers.append('Content-type', 'application/x-www-form-urlencoded');

    return'http://localhost:80/ang2router/public/addContact.php', params,{
      headers: headers
    .map(res => res.json());


this is hard coded for testing obviously.
then i call it on a button click:
        data => this.postData = data,
        error => alert(error),
        () => console.log("Finished")

then the php file that insert it into the database and get it back:
$value = json_decode($_POST['user']);
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json');

        $db_name  = 'mydb';
        $hostname = 'localhost';
        $username = 'myusername';
        $password = 'mypass';

        // connect to the database
       $dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);

    // a query get all the records from the users table
    $sql = 'INSERT INTO markers ( name, address,lat, lng ) 
        VALUES ( "' . $value->name .'" , "'.$value->address.'", '.$value->lat.' , '.$value->lng.' );';

    $stmt = $dbh->prepare( $sql );

    $check = $stmt->execute();

    $last_id = $dbh->lastInsertId();

    $sql2 = "SELECT * FROM markers WHERE id=$last_id ";

    $stmt = $dbh->prepare( $sql2 );


    // fetch the results into an array
    $result = $stmt->fetchAll( PDO::FETCH_ASSOC );
    echo json_encode($result);

in firefox all goes well, the data is in the database and i get the last inserted id back.
but on chrome not. i attach what i get on chrome.

best regards

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros