0

I have some data from DB then I need to load. Something like this

$.ajax({
    type: "POST",
    url: "something.aspx",
    data: "{ 'something': 'something'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(res) {}
});​

I want returning string res.d to be added to var a, how this can be achieved.

$(document).ready(function () {
    var a =
});

Basically I am using xDHTML schedule unit view. Schedule units needs to be loaded on page load. So how to ad string from DB to var

karim79
  • 339,989
  • 67
  • 413
  • 406
Nikola Gaić
  • 117
  • 1
  • 11

1 Answers1

3
var a = null;

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "something.aspx",
        data: "{ 'something': 'something'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(res) {
            a = res;
        }
    });​
});

Should do it? Did you need the ASP part?

crush
  • 16,713
  • 9
  • 59
  • 100
  • I already tried that. When I put alert(a) below a = res I got good data, but when I put alert(a) after $.ajax({});​ I will get null – Nikola Gaić Feb 16 '12 at 14:25
  • I edited my script, this should work since a is now at global scope. – crush Feb 16 '12 at 14:32
  • Same thing :( inside $.ajax({}) everything is ok, but after it is constantly null. I don't know I will try tu put rest of code in success: function(res) {} – Nikola Gaić Feb 16 '12 at 14:44
  • I found this that real problem is [link](http://stackoverflow.com/questions/905298/jquery-storing-ajax-response-into-global-variable) is actually that var a = null; is not waiting for success: function(res) – Nikola Gaić Feb 16 '12 at 14:52
  • AJAX calls are asynchronous (asynchronous javascript and xml). You can set it to Synchronous if you need to block the thread until success returns, but I wouldn't advise it. If you need it to by synchronous, set async = false in the options of the ajax() call. – crush Feb 16 '12 at 14:53
  • Tnx, actually that was problem. I didn't know that that is working that way. – Nikola Gaić Feb 16 '12 at 15:02