How can I determine why a jQuery ajax $.post request wasn't successful?

Tags: post ajax jquery
Question!

I'm trying to determine, based on the result of this call, if it was successful. The successFunction doesn't get called, so I'm assuming it was not. How do I know what went wrong?

xmlRequest = $.post("/url/file/", { 'id' : object.id }, successFunction, 'json');

Do I use the xmlRequest object?



Answers

You can use:

$.ajax({
    url:"/url/file/",
    dataType:"json"
    data:{ 'id' : object.id }
    error:function(request){alert(request.statusText)}
    success:successFunction
})
By : defrex


Rails resource gives a RESTful interface for your model. Let's see.

Model

class Contact < ActiveRecord::Base
  ...
end

Routes

map.resources :contacts

Controller

class ContactsController < ApplicationController
  ...
  def show
    @contact = Contact.find(params[:id]

    respond_to do |format|
      format.html 
      format.xml {render :xml => @contact}
      format.js  {render :json => @contact.json}
    end
  end
  ...
end

So this gives you an API interfaces without the need to define special methods to get the type of respond required

Eg.

/contacts/1 # Responds with regular html page

/contacts/1.xml # Responds with xml output of Contact.find(1) and its attributes

/contacts/1.js # Responds with json output of Contact.find(1) and its attributes
By : JasonOng


There is a plugin that does just this, http://blog.labnotes.org/2007/12/11/json_request-handling-json-request-in-rails-20/

And from what I understand this functionality is already in Rails. But go see that blog post, there are code examples and explanations.

By : Evgeny


This video can help you solving your question :)
By: admin