0

\

I am new at this and I need some help. \

I want to create a Cordova project which is supposed to do this. On the main page there's a "Send"

button, when I click on it an ajax request should be sent to the the php file and it should return

"hello", and in the success function the result is alerted. Instead of that it alerts the whole PHP

file. This only happens when I run Cordova on browser with cmd. \

I tried to execute it like I would execute a php file and it worked, so I don't really understand what's

the problem here. \

Sorry for my bad English, but I hope that by looking at the photos you'll understand my problem.\

Help me Obi-Wan Kenobi, you're my only hope.\

alert.php

<?php echo json_encode("HELLO"); ?>

index.js

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
    // Cordova is now initialized. Have fun!

    console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
    document.getElementById('deviceready').classList.add('ready');
}

function send(){
$.ajax({
    type:"get",
    url: "alert.php",
    success: function(result){
    alert(result);
  }});
}    
document.getElementById("send").addEventListener("click",send);

https://i.stack.imgur.com/Pwl5m.png

after accessing http://192.168.0.111/Project/www/alert.php https://i.stack.imgur.com/q7BXK.png

ARAYIK
  • 9
  • 1
  • 1
    Please don't post text as image. Edit your question with the text and use the formatting option. See [ask] – Olaf Kock Jan 15 '21 at 18:00
  • _"I tried to execute it like I would execute a php file and it worked"_ - And how do you execute the php file when it worked? Through CLI? How are you serving the PHP files? Is the web server configured to execute PHP files at all? – M. Eriksson Jan 15 '21 at 18:11
  • @MagnusEriksson I used Xampp and accessed http://localhost/Project/www/alert.php and it worked. I didn't use the "cordova run browser" command. How do I know if it is configured to execute PHP files? I think that's something I missed, as I said I just started. – ARAYIK Jan 15 '21 at 18:19
  • Please do as @OlafKock said and edit your question to include your code as text in the question. – M. Eriksson Jan 15 '21 at 18:23
  • I did as you said, help me if you can, I am desperate – ARAYIK Jan 15 '21 at 18:47
  • Does this answer your question? [Apache cordova apk doesn't run properly on android](https://stackoverflow.com/questions/65761350/apache-cordova-apk-doesnt-run-properly-on-android) – Eric Jan 17 '21 at 18:05

2 Answers2

0

The problem is that http://192.168.0.111/Project/www/alert.php will just return the source code of alert.php that happens because the web server installed on the host 192.168.0.111 is not configured properly to run PHP.

Depending on the server installed on 192.168.0.111 check how to configure PHP with it( your webserver might be Apache, Nginx, etc).

If you configure the webserver properly when you visit http://192.168.0.111/Project/www/alert.php in the browser you should see just HELLO.

Andrei O.
  • 116
  • 1
  • 1
  • 5
  • when I open http://192.168.0.111/Project/www/alert.php link after activating xampp it works. Thank you for your answer. Do you know what I should do to make it work? – ARAYIK Jan 15 '21 at 18:24
  • If it works in the browser it should work also in an ajax request, maybe there's some kind of cache try remaining the file to `alert2.php` and then try again, there's no reason why it should not work inside ajax if it works in a browser. – Andrei O. Jan 15 '21 at 18:34
  • O I tried as you said, renamed the file and the ajax url, but it didn't work, same problem – ARAYIK Jan 15 '21 at 18:44
  • when I sent the ajax request to 192.168.0.111/project/www/alert2.php it gives me this error – ARAYIK Jan 15 '21 at 19:08
  • Refused to connect to 'http://192.168.0.111/Project/www/alert2.php' because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback. – ARAYIK Jan 15 '21 at 19:08
  • When you have a CSP error you need to configure the Cordova project to allow content from other sources, this link might help: https://stackoverflow.com/questions/37398695/cors-cordova-issues-with-access-control-allow-origin – Andrei O. Jan 15 '21 at 20:18
0

Reason why your code works on a localhost is because your file extension ends with .php

In your case http://192.168.0.111/Project/www/alert.php

You can not execute PHP code if file extension ends with .html

That is why you get whole content of a alert.php instend of method output

I would suggest you to use JSON format to return content of a PHP file and then handle that JSON in success() callback. In this case your JS code does not need refactoring but for a future reference please check out https://www.w3schools.com/js/js_json_intro.asp

Your alert.php file should look something like this:

    <?php 
        $txt = "HELLO";

        $json = json_encode($txt);

        echo $json;
    ?>
Luka
  • 69
  • 8