• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jQuery Ajax Request

 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to ajax, and just would like to ask an idea on how to implement this requirement.

Assuming, user is filling up a form and on the UserID field, I want to fire an ajax request to the server to check if it is existing so that I could notify him that it is already taken.



Now, I dont know how to go about doing this request.

I am thinking between two solution that I have thought.


Option 1:

If it exist, throw an exception and let the error callback of the jquery ajax call handle the request.


Option 2:

Return a JSON response from the server with below format parse it and then set the content appropriately.




I am not sure which one is the better solution or I maybe there is a better alternative other than what I have thought.
Looking forward for your thoughts..
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you throw an exception? Seems like a poor choice.

Eric
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Eric. An exception is complete overkill.

Use JSON, and use getJson() to simplify your code. $.ajax() should only be used when you absolutely have to in order to use an odd combination of options.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear and Eric,

I used $.ajax so that I could test the callback of the ajax call if ever an exception has happened.

Anyhow, regarding my question, I think option 1 is really an overkill and option 2 would be better.

Thanks to both of you..


P.S. How could you go wrong if two great authors are out here helping you..
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mark reyes wrote:I used $.ajax so that I could test the callback of the ajax call if ever an exception has happened.


You don't need to use $.ajax() to catch errors. You can establish an error handler separately, and it will be called for any Ajax request that results in an exception or other error status.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

You can establish an error handler separately, and it will be called for any Ajax request that results in an exception or other error status


I havent read about this yet.

Can you give me a hint on how can I configure this?

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://api.jquery.com/ajaxError/
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

Thanks. I will read more and test about this.

 
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic