jquery奇怪的行为

时间:2010-08-13 12:43:46

标签: jquery codeigniter post

我正在使用codeigniter框架,在视图中我正在使用jquery。现在,我有一个包含数据库值的选择框。我使用jquery change事件和警告框测试了它的值。它在更改警报框中给出了确切的值,但是当我使用post方法或获取甚至$ .ajax()时,它没有给出任何输出。事实上,我检查了console.log(),它也没有进入内部。我只需要发布一些值并从数据库中获取一些信息,以便在选择框下方的div中显示。 这是jquery的代码:

$(document).ready(function(){
   $('#org_name').change(function(){
       $.post('index.php/contact/getorg',{'query':$('#org_name').val()},function(data){
         console.log("inside post"); 
         $('#showorg').html(data);
         console.log(org_name);
       });//close post function
   }); //close org_name event function
});

3 个答案:

答案 0 :(得分:1)

尝试在方法参数中使用.ajax jQuery方法和指定的失败函数(error变量)。如果服务器端出现问题或者您有其他特定错误,您将能够分析XMLHttpRequest,textStatus,errorThrown变量

答案 1 :(得分:1)

我总是使用以下风格......

ajax({
   type: "POST",
   url: 'index.php/contact/getorg',
   data: JSON.stringify({'query':$('#org_name').val()}),
   dataType: "json",
   contentType: "application/json; charset=utf-8",
   success: function (data) {
      $('#showorg').html(JSON.parse(data.d));
   },
   error: showError
};

function showError(responseText, statusText, xhr, $form) {
            debugger;         }

编辑:

console.log(org_name);似乎也不正确。成员org_name来自哪里?

答案 2 :(得分:0)

感谢响应人员,错误回调方法对我帮助很大。它给了404方法,所以我改变了网址。现在它的工作就像魅力。让我与大家分享我所做的一切:

$('#org_name').bind('change',function(){
  $("#showorg").html("wait...");
  $.ajax({
    url: 'http://localhost/ifes/index.php/contact/getorg',
    type: 'POST',
    dataType: 'html',
    data:{'query':$('#org_name').val()},
    timeout: 1000,
    error: function(xhr,err){
    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
    alert("responseText: "+xhr.responseText);
    },
    success: function(data){
        $('#showorg').html(data);// do something with data
    }
  });  
});
相关问题