dimanche 28 juin 2015

jQuery AJAX response JSON get child node

i am trying to take the responseJSON from an AJAX call and just extract one element to the variable formDigestValue. I have tried about a dozen ways of trying to return the response, using JSON.parse(), $.parseJSON() and some others, but there are 2 main issues that i cant seem to figure out. I put in a check for if (data.lenght > 0){do something}, response.length, responseJSON, jqXHR, XHR, i cant seem to find the variable that holds the data that would end up sent to success function. I've tried just setting the ajax call to a variable (var y = $.ajax()...) and manipulating it that way.

I just keep reading different articles and trying different ways, but nothing seems to quite get it right and it seems to be fairly simple, but i feel like im just missing something simple on this.

    $(document).ready(function () {

        var siteURL = "xxx";
        var formDigestValue = "";

        jQuery.ajax({
                url: siteURL + "/_api/contextinfo",
                type: "POST",
                headers: { 
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                },
                success: function(){
                    contextHeaders = $.parseJSON(responseJSON);
                    formDigestValue = contextHeaders.FormDigestValue;
                }
            });
...

any advice would be greatly appreciated. For reference, the JSON returned looks like the below. i am trying to figure out if i also need anything extra to get at child nodes, as it looks like it comes back buried a bit (i broke it out with indents just to show the depth):

{
    "d":
    {
        "GetContextWebInformation":
        {
            "__metadata":
            {
                "type":"SP.ContextWebInformation"
            },
            "FormDigestTimeoutSeconds":1800,
            "FormDigestValue":"0xADC9732A0652EF933F4dfg1EF9C1B75131456123492CFFB91233261B46F58FD40FF980C475529B663CC654629826ECBACA761558591785D7BA7F3B8C62E2CB72,26 Jun 2015 21:20:10 -0000",
            "LibraryVersion":"15.0.4631.1000",
            "SiteFullUrl":"http://example.com/",
            "SupportedSchemaVersions":
            {
                "__metadata":
                {
                    "type":"Collection(Edm.String)"
                },
                "results":["14.0.0.0","15.0.0.0"]
            },
            "WebFullUrl":"http://www.example.cm"
        }
    }
}

edit 6/27

Alright, I think between the comment on accessing the child nodes and the rest on passing the argument to success function, ive almost go it. My main thing is, I cannot seem to pass it as an argument. I tried to say it initially, but dont think I write the explanation properly. I tried:

Success: function(responseJSON)...

As well as

Success: function(data)...

But the data never seems to actually enter the function, its null values. I know the json returned, but having issues passing it into success function

Here is a look at firebug when this runs: Function Error

Aucun commentaire:

Enregistrer un commentaire