'jquery post and get methods not working on remote server

the following jquery code works fine on local server but doesn't work on remote server. FYI remote server is recognizing jquery. I got that working through My Stackoverflow Q

Please checkout the problem page at Merry Flowers Admission Page. When I enter [email protected] as email id (in Parent Info) and press the tab key, all the subsequent form elements should be automatically filled. But it is not doing that here.

The following are the network headers from chrome's developer tools for /students/get_parent_info:

Request URL:http://www.merryflowers.com/students/get_parent_info
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:52
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:CAKEPHP=b0103aa50047806a7063301569298541
Host:www.merryflowers.com
Origin:http://www.merryflowers.com
Referer:http://www.merryflowers.com/students/add
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
X-Requested-With:XMLHttpRequest
Form Dataview URL encoded
data[MerryParent][email]:[email protected]
Response Headersview source
Connection:Keep-Alive
Content-Type:text/html; charset=UTF-8
Date:Fri, 20 Apr 2012 18:56:03 GMT

Keep-Alive:timeout=5, max=100
P3P:CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Server:Apache
Transfer-Encoding:chunked
X-Powered-By:PHP/5.2.17

network response and preview:

****************************************

FYI, I've already verified that there is existing record for [email protected].

After reading lazerblade's answer, I checked index.php and test.php on www and public_html folders. My root on remote server is /home/aquinto1. I made changes to these files long back. I didn't modify anything here now.

Below are my codes:

index.php

if (!defined('ROOT')) {
    define('ROOT', DS.'home'.DS.'aquinto1');  
    //define('ROOT', dirname(dirname(dirname(__FILE__))));
}
/**
 * The actual directory name for the "app".
 *
 */

if (!defined('APP_DIR')) {
    define('APP_DIR','app');
    //define('APP_DIR', basename(dirname(dirname(__FILE__))));
}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 *
 */

if (!defined('CAKE_CORE_INCLUDE_PATH')) {
    //define('CAKE_CORE_INCLUDE_PATH', ROOT);
    define('CAKE_CORE_INCLUDE_PATH', DS.'home'.DS.'aquinto1');

}

test.php

if (!defined('ROOT')) {
    define('ROOT', DS.'home'.DS.'aquinto1');  
    //define('ROOT', dirname(dirname(dirname(__FILE__))));
}
/**
 * The actual directory name for the "app".
 *
 */

if (!defined('APP_DIR')) {
    define('APP_DIR','app');
    //define('APP_DIR', basename(dirname(dirname(__FILE__))));
}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 *
 */

if (!defined('CAKE_CORE_INCLUDE_PATH')) {
    //define('CAKE_CORE_INCLUDE_PATH', ROOT);
    define('CAKE_CORE_INCLUDE_PATH', DS.'home'.DS.'aquinto1');
}


<script type="text/javascript">
   //var j=jQuery.noConflict();
  $(document).ready(function(){
      $("#MerryParentEmail").change(function(){
        //txt=$("#MerryParentEmail").val();
        email_id=$("#MerryParentEmail").serialize();
        $.post("/students/get_parent_info",email_id,function(result_str){
        result_array=result_str.split('*****');
          $("#MerryParentInitial").val(result_array[0]);
          $("#MerryParentName").val(result_array[1]); 
          $("#MerryParentLandline").val(result_array[2]);
          $("#MerryParentMobile").val(result_array[3]); 
          $("#MerryParentAddress").val(result_array[4]);
          $("#MerryParentStateId").val(result_array[5]);
          state=result_array[5];
          txt_str="state_id="+state;
          $.get("/students/getcities",txt_str,function(result){
            $("#MerryParentCityId").html(result).show();
            $("#MerryParentCityId").val(result_array[6]);
          });
          $("#MerryParentPostalCode").val(result_array[7]);
        });
      });

       $("#MerryParentStateId").change(function(){
        state=$(this).val();
        txt_str="state_id="+state;
        $.get("/students/getcities",txt_str,function(result){
            $("#MerryParentCityId").html(result).show();
        });
       });
 });
 </script>

students_controller's get_parent_info func and getcities func:

function get_parent_info(){
//$this->layout=false;
 if (!empty($this->data)){

    $merryparent_info=$this->Student->MerryParent->getMerryParents($this->data['MerryParent']['email']);
    print_r($merryparent_info);
    echo $merryparent_info['MerryParent']['initial'].'*****';
    echo $merryparent_info['MerryParent']['name'].'*****';
    echo $merryparent_info['MerryParent']['landline'].'*****';
    echo $merryparent_info['MerryParent']['mobile'].'*****';
    echo $merryparent_info['MerryParent']['address'].'*****';
    echo $merryparent_info['MerryParent']['state_id'].'*****';
    echo $merryparent_info['MerryParent']['city_id'].'*****';
    echo $merryparent_info['MerryParent']['postal_code'].'*****';
    }
}

function getcities(){
    $this->data['MerryParent']['state_id']=$_GET['state_id'];
    if (!empty($this->data['MerryParent']['state_id'])){
       $cities = $this->Student->MerryParent->City->getCities($this->data['MerryParent']['state_id']);
    //print_r($cities);
    foreach ($cities as $k=>$v){
            echo '<option value="'.$k.'">'.$v.'</option>';
    }

        /* foreach($cities as $optionValue){
            echo '<option>' . $optionValue . '</option>';
        }*/
    }else{
        $this->Session->setFlash('You didn\'t select a state!');
    }

}


Solution 1:[1]

By default, you can't make XHR requests across different domains.

You'll need to dynamically generate script tags and use JSONP.

Here's an article that seems to cover how to do it: http://cjihrig.com/blog/remote-ajax-calls-using-jsonp/

Also, it's important to note that this can cause security issues.

Solution 2:[2]

I believe the issue here has a lot to do with the differences between your localhost file structure and root path location and your remote server file structure and root path location. First, your url, even for AJAX calls, shouldn't be pathed from root. Your public folder (www, public_html) should redirect traffic into your framework, so you maintain SEO-friendly urls and a more secure site, in the event that PHP should somehow fail. That being said, your AJAX url should still be www.merryflowers.com/students/get_parent_info, where students is your controller and get_parent_info is your function in your students controller. If it's working locally, then it should work remotely, but it needs the correct pathway to get where it needs to go. I'm guessing your local setup includes an htdocs folder, whereas your remote server, as you stated, includes www and public_html folders (possibly simlinked). Start by checking your code for pathway reference differences - pathways locally that don't exist remotely, differences in folder structure, etc... Then go through your config, bootstrap, and any other files that define pathways and alter them accordingly. I'd also retag your question with MVC, PHP, Apache (if you're using Apache, or ISAPI, or whatever), htaccess, and path.

Solution 3:[3]

Ok, the problem was with the code in MerryParent Model. There was nothing wrong in my jquery code.

In MerryParent model,

function getMerryParents($field_value){         
if (is_int($field_value))         
    $conditions=array('merryParent.id'=>$field_value);         
else         
    $conditions=array('merryParent.email'=>$field_value);         

//debug($conditions);         

$merryparent_info=$this->find('first',array(         
                            'conditions'=>$conditions,         
                            'recursive'=>-1   //fetches merry_parents table data only not the associated data         
                            ));         
      debug($merryparent_info);         
return $merryparent_info;         
}         

I changed merryParent.id to MerryParent.id and merryParent.email to MerryParent.email and it works now on the remote server. :)

Solution 4:[4]

This worked for me.. i faced same problem but get it solved by following two steps

1). while referring jquery and other js files I changed to IP address instead relative path

2) in all http get requests i replaced localhost with correct IP address.

Thanks Lazerblade. You helped me a lot.

Mukharjee

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Hagianis
Solution 2 Lazerblade
Solution 3 vaanipala
Solution 4 Mukharjee