This discussion is archived
4 Replies Latest reply: Jul 11, 2013 7:32 AM by Spam RSS

jQuery JSON set items

Spam Newbie
Currently Being Moderated

Hi all,

 

I used to use the less functional ajax call using:

 

var ajaxRequest = new htmldb_Get(null, &APP_ID., 'APPLICATION_PROCESS=APP_MY_OD', 1);
//add all my variables I wish to access in my OD
ajaxRequest.add('P1_X', $v('P1_X'));
...
data = ajaxRequest.get();
//my OD then sets a string formatted JSON for return using apex_util.json_from_items('P1_X') and adds to session
//with the return string in JSON format set the items value using json_SetItems
 json_SetItems(data);

 

So I understand that the function json_SetItems parse the string when in JSON format.

 

My question, is there a similar json_SetItems function that will set items when a jQuery object JSON is returned? The one that's returned from the following:

 

$.ajax({
  type: 'POST',
  data:
// etc...
success: function (data) {
    //data is now a jQuery object, are there any built in functions for this?

 

 

I understand I could always make the dataType 'text' which would return a string and call the json_SetItems but was hoping to use dataType 'JSON'

 

Thanks

Spam

  • 1. Re: jQuery JSON set items
    Tom Petrus Expert
    Currently Being Moderated

    "data" is not a jQuery object. It is the returned data in a type specified by the dataType parameter. jQuery docs:

    dataType (default: Intelligent Guess (xml, json, script, or html))

    Type: String

    The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

     

    The success parameter is defined as this:

    success

    Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )

    A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object.

     

     

    So I'm not sure what exactly it is you're after. If you want plain text back then request it as such. If you'd rather have JSON then go with that.

  • 2. Re: jQuery JSON set items
    Spam Newbie
    Currently Being Moderated

    My question is if I return JSON then is there a jQuery / Javascript function that I can call to set Page items the result of the JSON, just like the json_SetItems funciton?

     

    If that makes sens

  • 3. Re: jQuery JSON set items
    Tom Petrus Expert
    Currently Being Moderated

    json_setItems doesn't really do that much. It iterates over the json object and sets the item's value.

     

    function json_SetItems(gReturn){
        gReturn = eval('(' + gReturn + ')');
        for (var j=0,len=gReturn.item.length;j<len;j++){
            apex.item(gReturn.item[j].id).setValue(gReturn.item[j].value);
        }
    }

     

    Basically, if you get a JSON object first, then either reconvert it into a string by using JSON.stringify(object) and passing it to json_SetItems (meh) , or use the code from the function above directly eg:

        for (var j=0,len=data.item.length;j<len;j++){
            apex.item(data.item[j].id).setValue(data.item[j].value);
        }
  • 4. Re: jQuery JSON set items
    Spam Newbie
    Currently Being Moderated

    Thought so, thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points