Question

I'm working with Javascript. I have a JSON response from Mediafire API :

{
    "response": {
        "action": "session_token",
        "session_token": "dskldkshjkfsffkjdkslk3783c214a86e7kfkdo",
        "result": "Success"
        "current_api_version": "2.13"
    }
}

I want to retrieve the response then save session_token in a variable.

I've tested this but it doesn't work:

Javascript :

<script>
   $(document).ready(function(){
  $("button").click(function(){
    $.getJSON("MY_LINK",function(response){
     var st = response.session_token;
     alert(st);
    });
  });
});
</script>

HTML :

<button>Get Json</button>

How can I do this ?

Was it helpful?

Solution

As per your callback function signature, you're receiving a variable named response. That is the entire returned object. That object has a property of its own named response. You need to access response.response.session_token (or edit the returned object to return only what's after response: if response.session_token is what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top