How to access json from Rest API











up vote
-1
down vote

favorite












I am returning data from a rest API. I was getting the header along with the json, but as Justin T. pointed out below I needed to add this to my cURL:



curl_setopt( $ch, CURLOPT_HEADER, 0);


That got rid of the header. Now I am having issue with the returned json and the encoding of it(I think)



if ( $httpCode != 200 )
{
echo "Return code is {$httpCode} n".curl_error($ch);
}
else {
$output = json_encode($result);
echo($output);
}


and my jQuery



$.ajax({
type:$('#BTA_AddUser').attr('method'),
url: form.action,
data: dataString,
dataType:"json",
success: function(data){
if(data) {
$('#response').html(data);
} else {
$('#response').html('<p>no response</p>');
}
}
});


This prints out the returned json on the page:



{"code":"UserUpdated","status":200,"message":"OK","payload":{"email":"test@test.com","expired":null,"funded":true}}


however, I cannot access the json objects, using



success:function(data){
if(data.code == "UserUpdated"){
//do something
}
}


It defaults to the else clause, not doing what is inside when looking for data.code










share|improve this question




















  • 1




    I didn't down vote.
    – vivek_23
    Nov 13 at 20:28






  • 1




    Also, working curl stuff above doesn't give us a good hint of what's happening above.
    – vivek_23
    Nov 13 at 20:30






  • 1




    @vivek_23, in the OP's defense, I believe they intentionally omitted the 'working curl stuff' because they did not want to post more code than was necessary. If they had suspected the issue was with the curl options, I am sure they would have posted it and not labeled it as 'working'. That being said, I still had enough information to know what they needed to do to resolve the issue...
    – Justin T.
    Nov 13 at 20:35








  • 1




    I should have put the curl options, my bad @vivek_23. Justin was able to gather what he needed. I'll do better. Thanks guys. Having other issues accessing the json, strange stuff.
    – Dirty Bird Design
    Nov 13 at 20:41






  • 1




    @DirtyBirdDesign Is the response you are getting from the API already in JSON? If yes, why are you encoding into JSON again?
    – vivek_23
    Nov 13 at 21:04















up vote
-1
down vote

favorite












I am returning data from a rest API. I was getting the header along with the json, but as Justin T. pointed out below I needed to add this to my cURL:



curl_setopt( $ch, CURLOPT_HEADER, 0);


That got rid of the header. Now I am having issue with the returned json and the encoding of it(I think)



if ( $httpCode != 200 )
{
echo "Return code is {$httpCode} n".curl_error($ch);
}
else {
$output = json_encode($result);
echo($output);
}


and my jQuery



$.ajax({
type:$('#BTA_AddUser').attr('method'),
url: form.action,
data: dataString,
dataType:"json",
success: function(data){
if(data) {
$('#response').html(data);
} else {
$('#response').html('<p>no response</p>');
}
}
});


This prints out the returned json on the page:



{"code":"UserUpdated","status":200,"message":"OK","payload":{"email":"test@test.com","expired":null,"funded":true}}


however, I cannot access the json objects, using



success:function(data){
if(data.code == "UserUpdated"){
//do something
}
}


It defaults to the else clause, not doing what is inside when looking for data.code










share|improve this question




















  • 1




    I didn't down vote.
    – vivek_23
    Nov 13 at 20:28






  • 1




    Also, working curl stuff above doesn't give us a good hint of what's happening above.
    – vivek_23
    Nov 13 at 20:30






  • 1




    @vivek_23, in the OP's defense, I believe they intentionally omitted the 'working curl stuff' because they did not want to post more code than was necessary. If they had suspected the issue was with the curl options, I am sure they would have posted it and not labeled it as 'working'. That being said, I still had enough information to know what they needed to do to resolve the issue...
    – Justin T.
    Nov 13 at 20:35








  • 1




    I should have put the curl options, my bad @vivek_23. Justin was able to gather what he needed. I'll do better. Thanks guys. Having other issues accessing the json, strange stuff.
    – Dirty Bird Design
    Nov 13 at 20:41






  • 1




    @DirtyBirdDesign Is the response you are getting from the API already in JSON? If yes, why are you encoding into JSON again?
    – vivek_23
    Nov 13 at 21:04













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am returning data from a rest API. I was getting the header along with the json, but as Justin T. pointed out below I needed to add this to my cURL:



curl_setopt( $ch, CURLOPT_HEADER, 0);


That got rid of the header. Now I am having issue with the returned json and the encoding of it(I think)



if ( $httpCode != 200 )
{
echo "Return code is {$httpCode} n".curl_error($ch);
}
else {
$output = json_encode($result);
echo($output);
}


and my jQuery



$.ajax({
type:$('#BTA_AddUser').attr('method'),
url: form.action,
data: dataString,
dataType:"json",
success: function(data){
if(data) {
$('#response').html(data);
} else {
$('#response').html('<p>no response</p>');
}
}
});


This prints out the returned json on the page:



{"code":"UserUpdated","status":200,"message":"OK","payload":{"email":"test@test.com","expired":null,"funded":true}}


however, I cannot access the json objects, using



success:function(data){
if(data.code == "UserUpdated"){
//do something
}
}


It defaults to the else clause, not doing what is inside when looking for data.code










share|improve this question















I am returning data from a rest API. I was getting the header along with the json, but as Justin T. pointed out below I needed to add this to my cURL:



curl_setopt( $ch, CURLOPT_HEADER, 0);


That got rid of the header. Now I am having issue with the returned json and the encoding of it(I think)



if ( $httpCode != 200 )
{
echo "Return code is {$httpCode} n".curl_error($ch);
}
else {
$output = json_encode($result);
echo($output);
}


and my jQuery



$.ajax({
type:$('#BTA_AddUser').attr('method'),
url: form.action,
data: dataString,
dataType:"json",
success: function(data){
if(data) {
$('#response').html(data);
} else {
$('#response').html('<p>no response</p>');
}
}
});


This prints out the returned json on the page:



{"code":"UserUpdated","status":200,"message":"OK","payload":{"email":"test@test.com","expired":null,"funded":true}}


however, I cannot access the json objects, using



success:function(data){
if(data.code == "UserUpdated"){
//do something
}
}


It defaults to the else clause, not doing what is inside when looking for data.code







php json ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 20:55

























asked Nov 13 at 20:14









Dirty Bird Design

2,194948102




2,194948102








  • 1




    I didn't down vote.
    – vivek_23
    Nov 13 at 20:28






  • 1




    Also, working curl stuff above doesn't give us a good hint of what's happening above.
    – vivek_23
    Nov 13 at 20:30






  • 1




    @vivek_23, in the OP's defense, I believe they intentionally omitted the 'working curl stuff' because they did not want to post more code than was necessary. If they had suspected the issue was with the curl options, I am sure they would have posted it and not labeled it as 'working'. That being said, I still had enough information to know what they needed to do to resolve the issue...
    – Justin T.
    Nov 13 at 20:35








  • 1




    I should have put the curl options, my bad @vivek_23. Justin was able to gather what he needed. I'll do better. Thanks guys. Having other issues accessing the json, strange stuff.
    – Dirty Bird Design
    Nov 13 at 20:41






  • 1




    @DirtyBirdDesign Is the response you are getting from the API already in JSON? If yes, why are you encoding into JSON again?
    – vivek_23
    Nov 13 at 21:04














  • 1




    I didn't down vote.
    – vivek_23
    Nov 13 at 20:28






  • 1




    Also, working curl stuff above doesn't give us a good hint of what's happening above.
    – vivek_23
    Nov 13 at 20:30






  • 1




    @vivek_23, in the OP's defense, I believe they intentionally omitted the 'working curl stuff' because they did not want to post more code than was necessary. If they had suspected the issue was with the curl options, I am sure they would have posted it and not labeled it as 'working'. That being said, I still had enough information to know what they needed to do to resolve the issue...
    – Justin T.
    Nov 13 at 20:35








  • 1




    I should have put the curl options, my bad @vivek_23. Justin was able to gather what he needed. I'll do better. Thanks guys. Having other issues accessing the json, strange stuff.
    – Dirty Bird Design
    Nov 13 at 20:41






  • 1




    @DirtyBirdDesign Is the response you are getting from the API already in JSON? If yes, why are you encoding into JSON again?
    – vivek_23
    Nov 13 at 21:04








1




1




I didn't down vote.
– vivek_23
Nov 13 at 20:28




I didn't down vote.
– vivek_23
Nov 13 at 20:28




1




1




Also, working curl stuff above doesn't give us a good hint of what's happening above.
– vivek_23
Nov 13 at 20:30




Also, working curl stuff above doesn't give us a good hint of what's happening above.
– vivek_23
Nov 13 at 20:30




1




1




@vivek_23, in the OP's defense, I believe they intentionally omitted the 'working curl stuff' because they did not want to post more code than was necessary. If they had suspected the issue was with the curl options, I am sure they would have posted it and not labeled it as 'working'. That being said, I still had enough information to know what they needed to do to resolve the issue...
– Justin T.
Nov 13 at 20:35






@vivek_23, in the OP's defense, I believe they intentionally omitted the 'working curl stuff' because they did not want to post more code than was necessary. If they had suspected the issue was with the curl options, I am sure they would have posted it and not labeled it as 'working'. That being said, I still had enough information to know what they needed to do to resolve the issue...
– Justin T.
Nov 13 at 20:35






1




1




I should have put the curl options, my bad @vivek_23. Justin was able to gather what he needed. I'll do better. Thanks guys. Having other issues accessing the json, strange stuff.
– Dirty Bird Design
Nov 13 at 20:41




I should have put the curl options, my bad @vivek_23. Justin was able to gather what he needed. I'll do better. Thanks guys. Having other issues accessing the json, strange stuff.
– Dirty Bird Design
Nov 13 at 20:41




1




1




@DirtyBirdDesign Is the response you are getting from the API already in JSON? If yes, why are you encoding into JSON again?
– vivek_23
Nov 13 at 21:04




@DirtyBirdDesign Is the response you are getting from the API already in JSON? If yes, why are you encoding into JSON again?
– vivek_23
Nov 13 at 21:04












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You need to set your CURLOPT_HEADER to false. This will exclude the plain-text header information from the response.






share|improve this answer























  • Awesome, I got rid of the header - now I just get the json object
    – Dirty Bird Design
    Nov 13 at 20:25










  • Please see updated question
    – Dirty Bird Design
    Nov 13 at 20:55













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288808%2fhow-to-access-json-from-rest-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You need to set your CURLOPT_HEADER to false. This will exclude the plain-text header information from the response.






share|improve this answer























  • Awesome, I got rid of the header - now I just get the json object
    – Dirty Bird Design
    Nov 13 at 20:25










  • Please see updated question
    – Dirty Bird Design
    Nov 13 at 20:55

















up vote
1
down vote



accepted










You need to set your CURLOPT_HEADER to false. This will exclude the plain-text header information from the response.






share|improve this answer























  • Awesome, I got rid of the header - now I just get the json object
    – Dirty Bird Design
    Nov 13 at 20:25










  • Please see updated question
    – Dirty Bird Design
    Nov 13 at 20:55















up vote
1
down vote



accepted







up vote
1
down vote



accepted






You need to set your CURLOPT_HEADER to false. This will exclude the plain-text header information from the response.






share|improve this answer














You need to set your CURLOPT_HEADER to false. This will exclude the plain-text header information from the response.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 21:46

























answered Nov 13 at 20:23









Justin T.

582313




582313












  • Awesome, I got rid of the header - now I just get the json object
    – Dirty Bird Design
    Nov 13 at 20:25










  • Please see updated question
    – Dirty Bird Design
    Nov 13 at 20:55




















  • Awesome, I got rid of the header - now I just get the json object
    – Dirty Bird Design
    Nov 13 at 20:25










  • Please see updated question
    – Dirty Bird Design
    Nov 13 at 20:55


















Awesome, I got rid of the header - now I just get the json object
– Dirty Bird Design
Nov 13 at 20:25




Awesome, I got rid of the header - now I just get the json object
– Dirty Bird Design
Nov 13 at 20:25












Please see updated question
– Dirty Bird Design
Nov 13 at 20:55






Please see updated question
– Dirty Bird Design
Nov 13 at 20:55




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288808%2fhow-to-access-json-from-rest-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?