weixin_33739523 2013-07-24 07:24 采纳率: 0%
浏览 31

在Ajax中解析Json,错误

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 that 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, and if jsonp is true, then ouput with the callback
 ** function; if not - just 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, I get 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 This Error]    

   ...ute(i),"string"==typeof r){try{r="true"===r?!0:"false"===r?!1:"null"===r?null:+r...

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

What I want 1. Parse the Json response. "Top Cat1" goes to list heading listings under it.

What I am doing wrong.

  • 写回答

1条回答 默认 最新

  • weixin_33717298 2013-07-24 09:30
    关注

    Why are you using the function json_encode in a string that is already 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 you 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 is should be used if you had something like 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);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站