1

I have this annoying problem with ajax in JQuery. I have a function where I want to get prices from server for bunch of products. The problem is that I have a function that is not invoked on succes, but rather it works on its own. So what am I trying to do is simply run the function, that calls server and gets data, then it traverse through element and update prices for the products.

The problem is that I don't know how to make AJAX call simply return me that JSON string. I am unable to make succes function return response data.

function getdataJSON(url, data, method, type) {
      $.ajax({
                url: url,
                method: method,
                type: type,
                data: "data=" + JSON.stringify(data) + "&ajax=1",
                success: function (response) {
                    readataJSON(response);
                },
//                error: function (response) {
//                  errordataJSON(response);
//                }
}

function readdataJSON(responseJSON) {
          return responseJSON;
}

function myFunc(element) {
        var url = "url_to_server";
        var data = "product_types";
        var type = "html";
        var method = "GET";        

        var data = getdataJSON(url, data, html, method); // i want my prices here returned from server in JSON format, but data is always empty
        decodedDATA = $.parseJSON(data); 
        // here goes functionality corresponding to element and retrived data
}

What am I doing wront or how can I make my function work as I described.

Thanks in advance.

DrCord
  • 3,917
  • 3
  • 34
  • 47
  • Maybe a typo: the successfuntion calls readateJSON not readdateJSON – user3154108 Apr 21 '15 at 13:59
  • html must be in string`var data = getdataJSON(url, data, "html", method);` – Brijesh Bhatt Apr 21 '15 at 14:00
  • 1
    There are quite a lot of typos (that can be picked up easily enough by bothering to look at the browser's console) but the main problem is a lack of understanding about what asynchronous means. – Quentin Apr 21 '15 at 14:02

0 Answers0