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"').

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

报告相同问题?

悬赏问题

  • ¥100 海康工业相机受其它打开端口影响,读取缓慢的问题。
  • ¥15 我想达到能够自行训练模型的一个程度,需要学多久(已有Python基础,但未接触其他)
  • ¥30 关于多级下拉列表获取数据库值内容的问题
  • ¥15 求mfc界面开发封装好的库
  • ¥15 comsol如何建立一个封闭气室膜片对温度变化产生的形变量仿真模型
  • ¥15 树莓派4B与stm32串口通讯
  • ¥15 C++后台循环截屏代码收集
  • ¥15 GD32F303读取ADS1118芯片传输的电压
  • ¥15 maple中的DEplot画相图,如何能清楚显示常微分方程组的两个极限环?
  • ¥15 C++后台循环截屏bug