JavaScript 返回“不是函数”错误,但该方法在 Firebug 中的响应中列出


JavaScript returns "is not a function" error yet the method is listed in response in Firebug

加载网页时,我在Firebug中收到以下错误:

redirect.processBrowser is not a function
[Break On This Error]   
url = redirect.processBrowser(JSON.stringify(browserInfo));

Firebug 中的响应如下所示:

{"transport":"POST","envelope":"JSON-RPC-2.0","contentType":"application'/json","SMDVersion":"2.0","target":"'/json-rpc.php","services":{"processBrowser":{"envelope":"JSON-RPC-2.0","transport":"POST","parameters":[{"type":"object","name":"json","optional":false}],"returns":"string"}},"methods":{"processBrowser":{"envelope":"JSON-RPC-2.0","transport":"POST","parameters":[{"type":"object","name":"json","optional":false}],"returns":"string"}}}{"error":{"code":-32600,"message":"Invalid Request","data":null},"id":null}

代码的 javascript 如下所示:

<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/json2.js" type="text/javascript"></script>
<script src="js/jquery.zend.jsonrpc.js" type="text/javascript"></script>
<script src="js/browserDetect.js" type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(function()
        {
            browserInfo = {
                "screen_width": screen.width,
                "screen_height": screen.height,
                "color_depth": screen.colorDepth,
                "url": document.URL,
                "user_agent": navigator.userAgent,
                "browser_name": BrowserDetect.browser,
                "browser_version": BrowserDetect.version,
                "platform": BrowserDetect.OS
            };
            redirect = jQuery.Zend.jsonrpc({url: '/json-rpc.php'});
            url = redirect.processBrowser(JSON.stringify(browserInfo));
            window.location.replace(url);
        });
    </script>

jsonrpc.php 代码如下:

<?php
// Define path to the application directory
defined('REFERRAL_SYSTEM_ROOT') || define('REFERRAL_SYSTEM_ROOT', realpath('/system'));
defined('REFERRAL_PUBLIC_ROOT') || define('REFERRAL_PUBLIC_ROOT', realpath('/public'));
require_once REFERRAL_SYSTEM_ROOT . '/some_file.php';
/**
 * Zend Application
 **/
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application($_ENV["REFERRAL_ENVIRONMENT"], REFERRAL_SYSTEM_ROOT . '/config/application.ini');
$application->getBootstrap();
require_once 'Browser.php';
// Instantiate server, etc.
$server = new Zend_Json_Server();
$server->setClass('Browser');
if('GET' == $_SERVER['REQUEST_METHOD'])
{
    // Indicate the URL endpoint, and the JSON-RPC version used:
    $server->setTarget('/jsonrpc.php')
           ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
    // Grab the SMD
    $smd = $server->getServiceMap();
    // Return the SMD to the client
    header('Content-Type: application/json');
    echo $smd;
}
$server->handle();

从响应中可以看出,进程浏览器被ZEND_JSON_RPC服务器看到,但我得到的是"不是函数"响应。我不知道为什么会发生这种情况。

有什么想法吗?

编辑 -> 4/26/2012 @ 4:25 PM EDT

虽然函数processBrowser()确实存在,但它是Browser.php中定义的Browser类的一部分,调用jQuery.Zend.jsonrpc({url: '/json-rpc.php'})显然没有收到正确的响应。我仍然不知道为什么。

我从火虫控制台中获取了您所说的 json,并对其进行了 jsonlint 处理。

结果如下:第 35 行的解析错误:...g" } }}{ "错误": {
--------------------^期待 'EOF'

, '}', ',', ']'

在错误部分之前,整个响应的右括号。 我不确定是什么产生了响应,但需要将其从那里删除并将其放在最后。

这是更正的 json:

{ "运输": "邮政", "信封": "JSON-RPC-2.0", "contentType": "application/json", "SMDVersion": "2.0", "target": "/json-rpc.php", "服务":{ "进程浏览器":{ "信封": "JSON-RPC-2.0", "运输": "邮政", "参数":[{ "类型": "对象", "名称": "JSON", "可选":假}], "返回":"字符串" } }, "方法":{ "进程浏览器":{ "信封": "JSON-RPC-2.0", "运输": "邮政", "参数":[{ "类型": "对象", "名称": "JSON", "可选":假}], "返回":"字符串" } } { "错误":{ "代码": -32600, "消息": "无效请求", "数据":空 }, "id":空 } };

我遇到了同样的问题。尝试删除行" header('Content-Type: application/json'); "。它对我有用:)