views:

56

answers:

2

Hi,

I'm using an ajax call to do a minor calculation then return the value and display it in the page same page where the form is submitted. In Firebug it says it calls the function, however doesn't get a response. (I have a similar form that writes to a database that works fine, seemingly because it doesn't need a response - firebug says it fails to get a response on that script as well.) The odd thing is that I wrote this on my local server before implementing it on the site and everything worked as planned. I'm using Code Igniter on both the local server and the web server, but I don't know if that has something to do with it. Anyways, any help would be great. I'm marginally new so this is kinda outta my realm at this moment.

Thanks

EDIT: .js

$(document).ready(function() {

    $('#submit').click(function(){

    var formdata = {
        years: $('#years').val(),
        rate: $('#rate').val(),
        principle: $('#principle').val(),
        periods: $('#periods').val(),
        continuous: $('#continuous').val()
        }

    $.ajax({
        url: "http://localhost:8888/CodeIgniter_1.7.2/index.php/timevalueshow/submit",
        type: 'POST',
        data: formdata,
        success: function(data){
                $('#replace').replaceWith('<p>'+data+'</p>');                       
        }
    });

    return false;
});


 });

php submit function

function submit(){

    $years = $this->input->post('years');
    $rate = $this->input->post('rate');
    $principle = $this->input->post('principle');
    $periods = $this->input->post('periods');
    $isCont = $this->input->post('continuous');

    $params = array(
        'years' => $years, 
        'rate' => $rate, 
        'principle' => $principle, 
        'periods' => $periods, 
        'isCont' => $isCont
    );

    $this->load->library('timevalue', $params);

    return $this->timevalue->FVFactor();
}
A: 

This works on the dev and not on the server because you are calling localhost!

// this will have the client call itself on this particular page (wont work)
url: "http://localhost:8888/CodeIgniter_1.7.2/index.php/timevalueshow/submit",

The above code should be just:

// this is relative to the document ROOT, will work on server but not on dev!
// you can set it relative to the calling page using ../ as needed
url: "/index.php/timevalueshow/submit",
Frankie
Hi, I accidentally copied the the local file instead of the file on the server. The call to the function goes through, there just isn't any response.
tshauck
@tshauck what I always do trying to kill the beat is to have the server **always** echoing something so you'll be sure that if the function is running **it is outputting something**. If you can I would also have an `error_log('function runs');` on php just to be sure. Please do that and let us know if even with the function returning something you still don't get an answer. Thks!
Frankie
Hi, so I tried that and I got no error message, however I wasn't sure I was setting it up right, so I ran a mail() inside the function to send me an email if it ran, and it did.
tshauck
A: 

Could it be that the request is being made cross-domain? Remember that mydomain.com is considered a different domain to www.mydomain.com.

I ran into a similar situation recently. I requested a page from mydomain.com which made an AJAX request to a script on www.mydomain.com. The request was not made because it was considered cross-domain. It had the same symptoms that you describe. In Firebug and Chrome Developer Console I saw an empty response and no error message.

The problem is that CodeIgniter generates absolute URLs based on the $config['base_url'] setting. If you access the site using a different domain name to what is configured in $config['base_url'] you can run into this type of problem.

Stephen Curran