0

how to get just json data,from this reponse

$.get('http://localhost:8000/json', function(response) {
  alert(response);
});

response is :

<div id="dom-target" style="display: none;">
    {"logo":"logo.jpg"}
</div>

how to get value inside response function, so i can get like alert(response.logo) means logo.jpg

4
  • 1
    Why don't you fix the format of your response to not have the html in it, and instead just return the JSON part? Commented Mar 19, 2017 at 10:41
  • i try to hide response from my codeigniter controller.. just that.. i know thats is bad idea.. any suggestion for me? Commented Mar 19, 2017 at 11:25
  • 1
    The response to an Ajax request doesn't get displayed in the page, it is returned directly to your JS, so there is nothing to hide. Again, remove the HTML and just return the JSON part. (I guess if the user entered that URL directly into their browser then it would display the JSON, but so what? You can't prevent the user seeing the JSON if they are determined.) Commented Mar 19, 2017 at 11:30
  • how stupid iam.. that's absolutely right.. thanks dude.. for make me realize it.. thanks dude.. yes so nubie right.. hehehe Commented Mar 19, 2017 at 11:36

1 Answer 1

1

The obvious answer is fix your response,
but, as a workaround, you can parse the string you get with jquery...

var str = '<div id="dom-target" style="display: none;"> {"logo":"logo.jpg"}</div> '

var el = $(str)

var inner = el.html()

console.log(JSON.parse(inner))

console.log(JSON.parse(inner).logo)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

wow.. thats cool... like you see the response is from echo inside codeigniter controller.. and i just wanna hide em with display=none, any suggestion for eficient and save passing variable from php to javascript? cos i newbe on this.. thanks maioman..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.