使用JQuery调用CodeIgniter控制器时出现Ajax 400 Bad Request

时间:2013-09-19 16:33:34

标签: php jquery ajax codeigniter

我有这个问题。我使用JQuery中的ajax来调用CodeIgniter控制器中的函数。但我总是得到400 Bad Request错误,即使我只是从函数打印纯文本。

以下是代码:

class Prueba extends CI_Controller {

//Displays de page, the code here is irrelevant
public function index(){
    $this->validar_sesion();

    $ciclos=  $this->recuperar_periodos_escolares();

    $this->smartyci->assign('secciones',  $this->session->userdata('secciones'));
    $this->smartyci->assign('modulos',  $this->session->userdata('modulosAutorizados'));
    $this->smartyci->assign('ciclos',$ciclos);

    $this->smartyci->display('v3/reportes/reporte_ranking_general.tpl');  
}

//The function to be called with AJAX, it originally has more code, 
//but for the tests I just echoed a plain string, which is never returned.
public function recuperar_instrumentos(){
    echo "EXITO!";
}

这是javascript:

var url="148.209.25.135/evaluacionAdmin/index.php/prueba;";


function recuperar_instrumentos(idCicloEscolar){
    var request=$.ajax({
        type:"POST",
        url:url,
        data:{idCiclo:idCicloEscolar}
    });

    request.done(function(data){
        $('#instrumento').html(data);
    });

    request.fail(function(xhr,textStatus,error){
        $('#instrumento').html(xhr.responseText);
        alert("status= "+textStatus+"\nerror= "+xhr.status+"\n"+error);

    });

我总是得到这个回应

jqXHR.responseText:
    An Error Was Encountered
    The URI you submitted has disallowed characters.

jqXHR.status: 400

textStatus: error

error: Bad request

对于那里发生的任何暗示或帮助都将非常感激

2 个答案:

答案 0 :(得分:1)

var url="148.209.25.135/evaluacionAdmin/index.php/prueba;";

在网址中有一个半冒号。将其更改为:

var url="148.209.25.135/evaluacionAdmin/index.php/prueba";

标准CI配置具有URI的允许字符列表。您可以修改正则表达式以接受半冒号,但在这种情况下,看起来您偶然将那个半冒号放在那里。

答案 1 :(得分:0)

问题出在您的网址中

var url="148.209.25.135/evaluacionAdmin/index.php/prueba;";

应该是

var url="148.209.25.135/evaluacionAdmin/index.php/prueba";// delete extra semicolon