Coderguy’s Blog

Thoughts on code

About

Hello, my name is Daniel Ice. I am a software developer in the Dallas, TX area. I enjoy learning about new technology. I mostly focus on PHP and Ruby on Rails.

I found myself needing a really quick way to do an ajax call and get the value returned to a single var. I did a lot of searching and found a lot of convoluted responses that involved callbacks.  Using callbacks would be overkill for what I was doing. After much searching I finally found this jQuery API comment. The comment explains how to just get a single value returned from an ajax call.  Here is the key points:

  1. Make sure the call is synchronized. (async: false)  This will allow you to wait for the response and not assign a blank value.
  2. Understand that when you make the $.ajax() call this the data that is normally passed to the callbacks is stored in $.ajax().responseText.

var response = $.ajax({ type: "GET", url: "check.php", async: false }).responseText;

Leave a Reply