Unable to use Monaca with $.ajax



  • Hi, I am currently developing an application using Onsen UI as well as jQuery. I used jQuery’s $.ajax function to call a php file in another folder and it was working fine on the web. But when I load my codes onto Monaca I am unable to call the php file in the application built any longer. The $.ajax codes are as follows:

    $.ajax({
        method: "POST",
        url: 'components/fpdf/generatePDF.php',
        data: data,
        success: function (response) {
            if (response.localeCompare("success") == 0){
                  alertMessage("Email sent successfully, you may view the PDF from the email sent to "+result.get("email")+".", "Success");
            }
            else{
                console.log(response);
                errorMesage("Email sending failed, please contact the administrator should the problem persists.");
            }
            modal.hide();
         }
    });
    

    The code intends to create a pdf file, save it in a folder, and sends it to someone via email. The response I am receiving from success is the entire lines of codes in the generatePDF.php and no email is sent. Can $.ajax from jQuery be used alongside Monaca, and if not, are there any alternative routes I can take to achieve what I require? I am currently not using Angular or React.

    Thank you.


  • Monaca

    That probably happens because Monaca does not support PHP. The best way to fix this is to host your PHP code on a remote source and perform the $.ajax call to that source.



  • //use XMLHttpRequest instead of ajax

    //function takes in URL with POST and sends t to server

    // var params = “name=ben&age=19”;
    //callbackserver is a function that you want to call back after geting response from the server

    function serverconnect(url,params,callbackserver){

    var xhr = new XMLHttpRequest();

    //var url = "http://dumppost.php";
    //var params = “name=ben”;

    xhr.open(“POST”, url, true);

    //Send the proper header information along with the request
    xhr.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);

    xhr.onreadystatechange = function() {//Call a function when the state changes.
    if(xhr.readyState == 4 && xhr.status == 200) {
    alert(xhr.responseText);
    //callbackserver(xhr.responseText);
    return 1;}else {return 0;}}
    xhr.send(params);;}



  • @bobnino Ajax works the issue is Monaca, Android, iOS, etc do not have built in PHP servers, so any server side processing will have to be done remotely. The ajax call will have to point to an actual web server and not locally regardless of the method used to invoke it.


Log in to reply