-1

Hi I'm trying to call a php from the angularjs control file.

Both PHP and Control file are in the same folders and inside www directory of Wamp and I'm calling php like this

`$scope.url='far_submit.php';`
$http.post($scope.url, {"name": $scope.fname, "email": $scope.email, "lname": $scope.lname}).
                 success(function(data, status) {...}  

I'm getting an error in browser like XMLHttpRequest cannot load file:///C:/wamp/www/reg/far_submit.php. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Where I'm doing wrong ?

Thank you.

Narasimha Maiya
  • 1,009
  • 4
  • 12
  • 35
  • 1
    You need a webserver of some kind. You cannot make ajax requests on the `file://` protocol. – CollinD Apr 13 '16 at 06:52
  • 2
    Then you need to hit your angularjs page via apache as well, not via `file://`. – CollinD Apr 13 '16 at 06:55
  • 2
    You cannot run php outside a webserver unless in command line `$scope.url='http://localhost/reg/far_submit.php';` will give you result. – Gopakumar Gopalan Apr 13 '16 at 06:55
  • Now I'm geting this error now `XMLHttpRequest cannot load http://localhost/reg/far_submit.php. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.` – Narasimha Maiya Apr 13 '16 at 06:58
  • Sounds like you have a fresh new question to ask. While you're at it, google `CORS` – CollinD Apr 13 '16 at 07:00
  • You need to access your application in the browser using http, not file (check the address bar) – madmuffin Apr 13 '16 at 17:08
  • OK in that case see my answer down below – Gopakumar Gopalan Apr 13 '16 at 17:08

2 Answers2

2

Your $scope.url should be,

$scope.url='http://localhost/reg/far_submit.php';

And your PHP file far_submit.php should start with

<?php
  header("Access-Control-Allow-Origin: *");
Gopakumar Gopalan
  • 1,187
  • 14
  • 22
-1

The php file should be processed and served with a web server like Apache, Nginx, IIS, etc.

And then your JavaScript should call the URL serving that PHP using an Ajax method

Community
  • 1
  • 1
Ali
  • 2,993
  • 3
  • 19
  • 42