Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax returns error but is success

I have the following 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);
}



});



}

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



    }
}

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?

like image 475
CI_Guy Avatar asked Sep 24 '11 23:09

CI_Guy


People also ask

What causes an ajax error?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.

How does ajax return success data?

The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds. The function takes three parameters, namely: The data returned from the server, formatted according to the dataType parameter, or the dataFilter callback function.

Is ajax successful deprecated?

The parameter is not being deprecated, the success method is. You can continue using success: function re-read it carefully.

What is success response ajax?

Response is the object passed as the first argument of all Ajax requests callbacks. This is a wrapper around the native xmlHttpRequest object. It normalizes cross-browser issues while adding support for JSON via the responseJSON and headerJSON properties.


1 Answers

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

{ success : true }

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

like image 118
Goran Obradovic Avatar answered Oct 03 '22 23:10

Goran Obradovic