开发者

.ajax posts and gets response on local server, no response on web host

开发者 https://www.devze.com 2023-01-12 00:52 出处:网络
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

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();
}


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.


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",
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号