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
php json ajax
|
show 6 more comments
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
php json ajax
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
|
show 6 more comments
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
php json ajax
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
php json ajax
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
|
show 6 more comments
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
|
show 6 more comments
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
You need to set your CURLOPT_HEADER
to false
. This will exclude the plain-text header information from the response.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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