javascript - Parsing Json in Ajax, error -


code :

$.ajax({         type: "post",         url: "api url",         data: json.stringify(user),         datatype : "json",         success: function(apiresponse) {             var session = apiresponse.sessionid;             console.log ("session : "+ session);              $.ajax({                 type: "get",                 url: "another url",                 datatype : "jsonp",                 contenttype: "jsonp",                 success: function(apiresponse) {                     console.log (apiresponse);                     jquery.each(apiresponse,function(){                          console.log (apiresponse);                                             });                      },                 error: function(apiresponse) {                     alert("error  : " +apiresponse);                 }             });         },         error: function(apiresponse) {             alert("error  : " +apiresponse);         } 

===========

php code return json data

<?php  $jsonp = false; if ( isset( $_get[ 'callback' ] ) ) {     $_get[ 'callback' ] = strip_tags( $_get[ 'callback' ] );     $jsonp              = true;     $pre  = $_get[ 'callback' ] . '(';     $post = ');';   } //isset( $_get[ 'callback' ] )   /* encode json, , if jsonp true, ouput callback  ** function; if not - output json. */  $json = json_encode( '{"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}' ); print( ( $jsonp ) ? $pre . $json . $post : $json ); 

another url returns following data

  {"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]} 

================ now, following error (also mentioning console.log resp)

   session : 67a47816-5a03-44f9-ab24-01e1e8d4aad1    {"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}     typeerror: invalid 'in' operand e    [break on error]         ...ute(i),"string"==typeof r){try{r="true"===r?!0:"false"===r?!1:"null"===r?null:+r... 

=======================

what want 1. parse json response. "top cat1" goes list heading listings under it.

what doing wrong.

why using function json_encode in string json encoded?

$json = json_encode( '{"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}' ); 

normally should use:

$json = '"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}'; 

json_encode should used if had that:

$arraytemp = array("top cat1"=>array(array("id"=>"cat1","name"=>"product1"),array("id"=>"cat2","name"=>"product 2")),"top cat2"=>array(array("id"=>"cat3","name"=>"product 3"),array("id"=>"cat4","name"=>"product 4")));  $json = json_encode($arraytemp); 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -