代码之家  ›  专栏  ›  技术社区  ›  tshauck

.ajax在本地服务器上发布并获取响应,在web主机上没有响应

  •  1
  • tshauck  · 技术社区  · 15 年前

    我使用ajax调用进行一个小的计算,然后返回值并将其显示在提交表单的同一页面中。在Firebug中,它说它调用函数,但是没有得到响应。(我有一个类似的表单,它可以写入一个运行良好的数据库,似乎是因为它不需要响应—firebug说它也无法在该脚本上获得响应。)奇怪的是,我在本地服务器上编写了这个表单,然后在站点上实现它,一切都按计划进行。我在本地服务器和web服务器上都使用了CodeIgniter,但我不知道这是否与此有关。不管怎样,任何帮助都会很好。我是个新来的,所以现在这不在我的范围之内。

    谢谢

    编辑:.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提交函数

    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();
    }
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Stephen Curran    15 年前

    可能是请求是跨域的吗?记得吗mydomain.com被认为是另一个域www.mydomain.com。

    我最近也遇到了类似的情况。我向你要了一页mydomain.com它向上的脚本发出了AJAX请求www.mydomain.com。未提出请求,因为它被视为跨域。它的症状和你描述的一样。在Firebug和Chrome开发人员控制台中,我看到一个空响应,没有错误消息。

    问题是CodeIgniter基于 $config['base_url'] 设置。如果您使用与中配置的不同的域名访问站点 $config['base\u url']

        2
  •  0
  •   Frankie    15 年前

    这在dev上有效,而不是在服务器上,因为您正在调用 本地服务器 !

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

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