1

I am working with AngularJS and checking how the $http function works. I used php on server side which will receive the data and put it into the localhost db. I can get the data correctly from the form, so I know my html is okay but I cannot send it to the localhost server. I googled with the error message (Access to restricted URI denied) and various blogs, stackoverflow Q/A suggest that I should add Access-Control-Allow-Origin & Methods in my php header which I did but did not solve my problem. Beneath I am adding my AngularJS code and php code. I am sure html is not relevant here. I ran my php file in my browser and it prints connected so php is connected with server correctly and each time it adds an empty row in the db.

AngularJS file:

(function()
{
var app = angular.module('db',[]);

app.controller("formData",['$http', function($http)
{
    this.data = {}; //empty data to store the values;
    this.value = {}; 

    this.update = function()
    {
        this.data = this.value;
        this.value = "";
        this.post();
        this.data = {};

    }

     this.post = function()
     {
        $http.post('/ssc.php',
        {
            'name':this.data.name,
            'phone':this.data.phone,
            'email':this.data.email,
            'address':this.data.address

        }).success( this.value.message = "success")

    }

}]);
})();

and my php code:

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 2016 00:00:00 GMT');
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');


$con=mysql_connect("localhost","root", "");
mysql_select_db("hablumi",$con);
if($con){
  echo "Connected";
}else{
  echo "Not Connected";
}


$name = $_GET['name'];
$phone= $_GET['phone'];
$email = $_GET['email'];
$address = $_GET['address'];
echo $address;
   $query="INSERT INTO blah(name,phone,email,address) VALUES('$name','$phone','$email','$address');";


if(mysql_query($query))
{
   echo "ok";     
}else{
  echo  mysql_error();
}
?>

Thanks in advance for help.

Error message from the browser:

"Error: Access to restricted URI denied
     pf/<@file:///C:/Users/Ibtehaz/Desktop/Simple%20Animation%20with%20AngularJS/angular.min.js:87:304
m@file:///C:/Users/Ibtehaz/Desktop/Simple%20Animation%20with%20AngularJS/angular.min.js:83:196
 k/f<@file:///C:/Users/Ibtehaz/Desktop/Simple%20Animation%20with%20AngularJS/angular.min.js:80:431
f/<@file:///C:/Users/Ibtehaz/Desktop/Simple%20Animation%20with%20AngularJS/angular.min.js:112:322
Je/this.$get</l.prototype.$eval@file:///C:/Users/Ibtehaz/Desktop/Simple%20Animation%20with%20AngularJS/angular.min.js:126:134

Je/this.$get

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Ibtehaz
  • 49
  • 10
  • you don't need access control headers for same-domain requests. Can you please give us the exact error message, where it occurs, and possibly the request/response headers? – Kevin B Feb 11 '15 at 16:01
  • I have given all my codes up there. and the error messages as well. – Ibtehaz Feb 11 '15 at 16:11
  • 1
    Don't work on the filesystem. Start a webserver instead. – Kevin B Feb 11 '15 at 16:13
  • Sorry. I did not understand. – Ibtehaz Feb 11 '15 at 16:13
  • 1
    In other words, you're currently running your index.html from `C:/Users/Someguy/Desktop/Somefolder/index.html` directly in your browser. Instead, start a webserver that points localhost to `C:/Users/Someguy/Desktop/Somefolder/` and use localhost. – Kevin B Feb 11 '15 at 16:14

0 Answers0