Angularjs post to PHP

I have this console error.

POST http://localhost:8100/send.php 404 (Not Found)

And these is my script JS

And script PHP send.php
image

Someone can help me?. Thanks.

send.php is obviously not available on http://localhost:8100/send.php - that’s what the Error message 404 - Not Found is telling you.

Is this both on the same computer?
Can you open http://localhost:8100/send.php in a normal browser?

Yes, There are in the same computer.

And when I try to put the url. The script of send.php it download automatically.I can’t open it in the browser.

The is this POST http://localhost:8100/php/send.php 404 (Not Found).

Ah wait. 8100 is the port of ionic serve. And now I also see that the php folder is part of your ionic project.

You have to put the PHP script into another folder that is served by a webserver. Ionic won’t take care of that. Mobile app (Ionic) and backend app (PHP via Webserver) are two different projects.

2 Likes

It’s true. One more last question. I collocated the php file in the web serve, but it show me this error.

“XMLHttpRequest cannot load http://localhost/php/correo.php. Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access.”

I don’t know. Maybe need add some more to my php file?

Thanks. Sujan12

Search CORS php htaccess
its will get help

1 Like

This should help:

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Headers: Authorization, Content-Type');
  exit;
}
header('Access-Control-Allow-Origin: *');
1 Like

You need to have a PHP server up and running, for a quick solution on windows install LAMP and drop the file where it tells you.

Then you need to enter the full url of the file, should be something like 127.0.0.1/yourfile.php, lamp will guide you.

Then you will most likely face a CORS issue (maybe not since it’s localhost), but if you get error codes 0 try @Sujan12’s solution

Hi guys, Really I can’t solve it. And these is my code.

This is my angular code:

var app = angular.module(‘starter’, [‘ionic’]);

app.controller(‘send_email’,function($scope,$http){

var config = {
    method:'POST',
    url:'http://www.hobbylacuesta.com/correo.php',
    data:'correo=alexis.970991@gmail.com',
    headers:{'Content-Type':'application/x-www-form-urlencoded'}
}

$scope.sendemail = function(){
    var response = $http(config);
    
    response.success(function(data,status){
        console.log('Done');
    });
    
    response.error(function(data,status){
        console.log('Error');
    });
}

});

This is my php file (It is in the server):

<? php

$message = "email: ".$_POST[“correo”];
$message = htmlspecialchars($mensaje);
$message = stripslashes($mensaje);

$to = “alexis.970991@gmail.com”;
$subject = “You have new email”;
$header = “From:alexis.970991@gmail.com”;
$enviar = mail($to,$subject,$message,$header);

?> 

And this is the error.
image

Do you believe could you help me? … Thanks a lot! :grin:

Just copy the code I posted on line two of your PHP - done.

2 Likes

Try to put this in your php file

<?php header("access-control-allow-origin: *"); ?>

or this in .htaccess file

<ifModule mod_headers.c> Header set Access-Control-Allow-Origin: * </ifModule>

1 Like