samedi 18 juin 2016

Ajax $.post("../application/controller/theScript.php" is appended to site root and not relative

I have a set of email functions something like what is below for popping up a window that will send emails.

.add sends a post request to the userController and returns the VIEW that is the email window.

.init is called and sets up the handlers for the email window.

.send is called when the send button is pressed in the email window to send the email.

var Email={
con:"../application/controllers/",
dialog:null,

init:function(){..
//add event handlers for email dialog

send:function(){
    var emailType=$("#emailType").prop("value");

    var email=$("#email").prop("value");
    if(!email){this.error("Please provide a valid email address");return false}

    var emailTitle=$("#emailTitle").prop("value");
    if(!emailTitle){this.error("Please provide a valid email title");return false}

    var emailBody=$("#emailBody").prop("value");
    if(!emailBody){this.error("Please provide a valid email body");return false}

    if(emailType && email && emailTitle && emailBody){
        $.post(Email.con+"userController.php",{
            sendMail:"true",
            emailType:emailType,
            email:email,
            emailTitle:emailTitle,
            emailBody:emailBody,
        });

        return true;
    }

    return false;
},

add:function(id){
        $("#"+id).on("click",function(e){
        $.post(Email.con+"userController.php",{
            showMail:"true",
            emailType:this.dataset.emailType,
        },
        function(data,status){
            if(status==='success'){
                $("body").append(data);
                Email.init();
            }
        });
    });

    return this;
},

}

The problem is with the send function as the path in the $.post ajax call is not correct. When add is called it finds the userController which is in a folder system something like

  • application
    • controllers -userController.php
  • public

So the path "../application/controllers/userController.php" steps back one out of public and down the path to the controller.

In send the path is always appended to the site root or public folder and returns something like http://theSite/application/controllers/userController.php

Why is the send ajax call not using a relative path from the site root to the controller in the once case?

There must be 50 other ajax calls that work correctly on the site.

Aucun commentaire:

Enregistrer un commentaire