dqtdz08206 2013-03-04 09:44
浏览 86
已采纳

意外的令牌,但是哪个令牌?

I have a link with a magento shop, which outputs the following json(please ignore the bogus values for testing purposes):

jsfiddle http://jsfiddle.net/ZkZ4D/

non pretty formatting, output by php

[[{"customer_address_id":"4","created_at":"2013-01-14 10:49:59","updated_at":"2013-01-14 10:49:59","city":"abc town","country_id":"NL","firstname":"john","lastname":"doe","postcode":"7091 eh","street":"mwhahah 47
mwhgahahahaha","telephone":"31645494440","is_default_billing":true,"is_default_shipping":true}],[{"customer_address_id":"4","created_at":"2013-01-14 10:49:59","updated_at":"2013-01-14 10:49:59","city":"abc town","country_id":"NL","firstname":"john","lastname":"doe","postcode":"7091 eh","street":"mwhahah 47
mwhgahahahaha","telephone":"31645494440","is_default_billing":true,"is_default_shipping":true}]]

pretty formatting for human reading

[
    [
        {
            "customer_address_id": "4",
            "created_at": "2013-01-14 10:49:59",
            "updated_at": "2013-01-14 10:49:59",
            "city": "abc town",
            "country_id": "NL",
            "firstname": "john",
            "lastname": "doe",
            "postcode": "7091 eh",
            "street": "mwhahah 47
mwhgahahahaha",
            "telephone": "31645494440",
            "is_default_billing": true,
            "is_default_shipping": true
        }
    ],
    [
        {
            "customer_address_id": "4",
            "created_at": "2013-01-14 10:49:59",
            "updated_at": "2013-01-14 10:49:59",
            "city": "abc town",
            "country_id": "NL",
            "firstname": "john",
            "lastname": "doe",
            "postcode": "7091 eh",
            "street": "mwhahah 47
mwhgahahahaha",
            "telephone": "31645494440",
            "is_default_billing": true,
            "is_default_shipping": true
        }
    ]
]

How do I get the above json?

php code

class ajax extends plantinaNLmagento
    {
    public function __construct()
        {
        parent::__construct();
        } 
    public function getCustomerAdressAjax()
        {
        $id = (int)$_GET['customerid'];
        $q = $this->db->query("SELECT * FROM `tbl_magento_users` WHERE `core_id`=:ID",array('ID'=>$id));
        $customeradresses = array();
        while($who = $q->fetchObject())
            {
            $x=$this->mage->call('customer_address.list',$who->magento_ID);
            array_push($customeradresses,$x); 
            array_push($customeradresses,$x);
            }
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Content-type: application/json');
        echo json_encode($customeradresses);
        }
    }

I'm pushing the $customeraddress twice for testing purposes.

Now if I paste the resulting json into jsonlint or other json validators it all says it's valid json.

When I use it int he function JSON.parse or jQuery.parseJSON I get a unexected token error, but it doesn't say which token or where, and since my json passes the valdation I'm totally at a loss which token it fails at.

I must be missing something in the category of facepalm but I simply can't find it...

error message SyntaxError: Unexpected token

  • 写回答

1条回答 默认 最新

  • dsaaqdz6223 2013-03-04 13:05
    关注

    You JSON data is perfectly valid but you must also make sure that your PHP script sends only the JSON data and nothing else (notice, warning, error, etc will break the JSON).

    To check, use your browser's development tools, FireBug, etc, and look at the network inspector tab to see the actual response sent by PHP. Fix them errors if necessary.

    As for your fiddle: JSON data cannot be used inside JavaScript strings as-is. At minimum you must escape the backslashes (e.g. the JSON "Hello World" should become '"Hello\ World"').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动