jQuery的AJAX返回错误,而是成功 [英] jquery ajax returns error but is success

查看:151
本文介绍了jQuery的AJAX返回错误,而是成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JS

function change_ajaxarea1(){
navigator.notification.activityStart();
$('#ajaxarea1').load('http://server.net/droiddev/backbone1/index.php/welcome/', function(){

navigator.notification.activityStop();
$('#ajaxarea1').css("height","auto");
//$('#ajaxarea1').css("overflow","hidden");
});

//navigator.notification.activityStart();
}


function postarticle(){

 $.post("http://server.net/droiddev/backbone1/", { headline: "John", article: "2pm" } );

 }


function addarticle(){

var headlinetemp=$('#headline').val();
var articletemp=$('#article').val();
$.ajax({
type:'POST',
dataType:'json',
url: "http://server.net/droiddev/backbone1/index.php/welcome/addarticle/",
data: { 'headline': headlinetemp, 'article': articletemp},
success:function(){
alert('success');
},
error:function(xhr){
alert(xhr.status);
}



});



}

和下面的控制器code:

and the following controller code :

  <?php 

class Welcome extends CI_Controller {


    public function index()
    {

    $data['query']=$this->site_model->get_last_ten_articles();
$this->load->view('partial1',$data);

    }

    public function addarticle(){

    $headline=$this->input->post('headline');
    $article=$this->input->post('article');
    $this->site_model->insert_entry($headline,$article);



    }
}

的addarticle()的JavaScript AJAX作品和支柱的瓦尔到服务器并进入分贝,然而,JavaScript的将运行误差函数代替的成功的功能。 HTTP响应code是200,我认为将使它运行成功的功能。有什么建议?

The addarticle() javascript ajax works and posts the vars to the server and into the db, however, javascript will then run the error function instead of the success function. The http response code is 200 which I thought would make it run the Success function. any suggestions?

推荐答案

您需要从控制器返回JSON,因为这是jQuery的期望。您可以返回,即

You need to return json from controller, as this is what jQuery expects. You can return i.e.

{ success : true }

或者你可以设置HTML作为数据类型(也许它会工作没有内容,但你可能需要返回的东西)

Or you could set html as data type (maybe it will work without content but you may need to return something)

这篇关于jQuery的AJAX返回错误,而是成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆