Rachid
Rachid

Reputation: 403

Display json result of an ajax call?

i'm trying to retrieve a json from an url using an ajax call and display it.

Here is the ajax call i'm using for the moment, which works fine as the result is diplayed in the dev condole in the browser

but when i try to display the simple json in a div ($("#jsonResultDisplay").html(result);) it just keeps the div blank

is there a beter way to do it and maybe display the result on its own on a plain page ?

var returnResult;
$.ajax(
    {
        type: "POST",
        url: myUrl,
        datatype: "jsonp",
        data: "param1=" + param1 +
              "&param2=" + param2,
        success: function (result) {
            log.debug(result);
            $("#jsonResultDisplay").html(result);
                returnResult = result;
        },
        error: function (req, status, error) {
            log.debug(" error: \n req: " + req + "\n status: " + status + "\n error: " + error);
        }
    });

And also when i try to save the result to use it outsite of the ajax call, the variable just stays null ??

i'm sure it must stupid questions but i'm beginning and could really use some help. Thanks in advance

Upvotes: 0

Views: 748

Answers (2)

Kris Erickson
Kris Erickson

Reputation: 33844

Your result isn't xml or something is it? Perhaps using .text(result) will show the results better if it is. BTW your way of sending data is kind of fighting against the way to do it jquery, use maps. So:

var returnResult;
$.ajax(
    {
        type: "POST",
        url: myUrl,
        datatype: "jsonp",
        data: {param1: param1, param2: param2},
        success: function (result) {
            log.debug(result);
            $("#jsonResultDisplay").text(result);
                returnResult = result;
        },
        error: function (req, status, error) {
            log.debug(" error: \n req: " + req + "\n status: " + status + "\n error: " + error);
        }
    });

Upvotes: 2

Guard
Guard

Reputation: 6955

Do you get the proper results in your debug call?

BTW, why manually composing data when you can use the hash?

And you should better use textarea with fixed-width font for JSON display

Upvotes: 0

Related Questions