Remote PHP access with Ajax - Return is the code of the PHP file itself

Asked

Viewed 1,520 times

2

I have a system as below: HTML on the client side, Javascript and PHP on the server side. The idea is to have a kind of Webservice, where an HTML page (it has to be HTML because it will run in an app made in HTML5 with Intel XDK) prompts information to the server, where it is PHP that makes the return.

The problem: When I run this way, the result is the PHP code, as below, IE, the server did not process anything, returns everything that is written in the PHP file.

<?php 
 echo $data = date("d/m/Y H:i:s ");
?>

If I carry index.html on the same server where the other files are, then the return is the date, which would be expected, IE, only runs if it is all on the same server, but what I need is a remote HTML file, running on any computer that accesses the PHP/JS service on the server.

What’s wrong with this setup? There’s a more practical way to do this?

On the local computer (C:/), I have the HTML file with the code:

<html>
<head>
<script src="http://code.jquery.c...n.js"></script>
<script src="http://www.example.c...S.js"></script>
</head>
<body>
<a href="javascript:test();">Clique aqui...</a>   
</body>
</html>

Server-side http://www.example.com i have a Javascript and a PHP:

Javascript file:

function test() {
    var jqxhr = $.ajax( "servidor.php" )

    .done(function(response) {
    alert( "success" );
    alert(response);
    })
    .fail(function(response) {
    alert( "error" );
    alert(response);
    })
    .always(function(response) {
    alert( "complete" );
    alert(response);
    });
}

PHP file:

<?php  
     echo $data = date("d/m/Y H:i:s ");
 ?>

Updating

I tested it in some situations, and now I’ve located where the problem is that I can’t get back from PHP. It is in the JS address, which cannot reach the PHP file on the server.

So it doesn’t work:

$.post('http://www.iodasistemas.meximas.com/JSON/servidor.php', 
    {comando:true}, function(data){

That’s how it works:

 $.post('servidor.php', {comando:true}, function(data){

In short, the whole point is that HTML always calls JS, but it can’t get into PHP. Whether it’s running all the files on the server, or a loose HTML on the computer. The error I mentioned occurred because I had a.php server file in the same local HTML folder, so it was this file that it read, not the server one. It seems that when loading the JS, it goes to work locally, and does not wheel remote.

How to Get Javascript Into PHP using a full address http://....?

  • 1

    I know it sounds absurd but, the server where PHP files are has PHP installed?

3 answers

1

I tried to do so in javascript:

function test(){

$.post('http://www.seusite.com.br/servidor.php', {comando:true}, function(data){

alert(data);

});
}

1

I appreciate the help of all. I managed to make it work. Below the codes.

I would just like someone to give me a light on how to pass a value to the PHP file on the server, and how to treat this value in PHP.

So I can make a request to the server and have a processed return.

HTML file + JS (can save anywhere or any computer running)

<script type="text/javascript"> 
var urlTeste = 'http://www.meuservidor.com/servidor.php?jsoncallback=?';
$(document).ready(function() {
//Mensagem enquanto não carrega a pagina
$('#resultado').html('Carregando...');

$.getJSON(urlTeste,null, function(data){
$('#resultado').html(data);   
});
}); 
</script>

PHP file (on www.meuservidor.com/server.php server)

<?php
$var = date("d/m/Y H:i:s "); 
echo $_GET["jsoncallback"] . '(' . json_encode($var) . ');';    
?>
  • 1

    Hello, welcome to Stack Overflow! Glad you were able to solve the problem using JSONP (which is the name of this callback technique you used). But this question ended up leaving much of the format of the site, which is quite different from a forum. The ideal would have been to post the solution to your first question, and open a separate new question to the question that arose from there. You ended up posting new questions in the answer area, creating a kind of dialogue. Please take a look at our [help] for more detailed guidance. I hope I can help with future questions! :)

0

You can try using json to see if you can return what you want.

$.getJSON("www.suapagina.com.br",
              function (data) {

                      alert(data.mensagem);
              }
            )
            .success(function() { alert("Sucesso"); })
            .error(function() { alert("Erro"); })
            .complete(function() {  alert("Completo"); });
  • I’ve tried it this way, but so it only runs if all the files are on the server. I could only run with remote HTML even with the above code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.