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

StackOverflow https://stackoverflow.com/questions/10340137

سؤال

I am getting the following error in Firebug when loading a web page:

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

The response as seen in Firebug is as follows:

{"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}

The javascript for the code is as follows:

<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>

The jsonrpc.php code is as follows:

<?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();

As you can see from the response, processBrowser is seen by the ZEND_JSON_RPC server, yet I am getting the "is not a function" response. I'm at a loss as to why this is happening.

Any thoughts?

EDIT -> 4/26/2012 @ 4:25 PM EDT

While the function processBrowser() DOES exist, it is part of the Browser class as defined in Browser.php, the call jQuery.Zend.jsonrpc({url: '/json-rpc.php'}) is evidently not receiving a proper response. I still do not know why.

هل كانت مفيدة؟

المحلول

I'm having the same issue. Try to remove the line "header('Content-Type: application/json');". It worked for me :)

نصائح أخرى

I took the json that you said was from the firebug console and jsonlint it.

Here is the result: Parse error on line 35: ...g" } }}{ "error": {
--------------------^ Expecting 'EOF', '}', ',', ']'

You have the closing bracket to the whole response before the error section. I am not sure what is generating the response but it needs to be removed from there and place it at the end.

This is the json corrected:

{ "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 } };

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top