JavaScript API - Uncaught SyntaxError: Unexpected token )


(Jamie Smith) #1

Hello,


I'm running Matrix v3.26.2 and attempting to use the JavaScript API. I thought I'd start simple with a getGeneral() call. But, alas, I'm getting the error "Uncaught SyntaxError: Unexpected token )" on line 75 of the API code:


    var jsonObj = eval('(' + json + ')');


The code from my page:

    
    
<script type="text/javascript">

setApiKey(<API key>);

$(document).ready(function() {

getGeneral(<asset number>, function(data) {
    if (isset(data.error)) {
            alert(data.error);                                        
    } else {
            alert(data.name);                                         
    }
});
});
</script></pre><br />

Error is the same for Chrome 4.1.249.1064, IE7 and FF 3.0.10

Can anyone shed any light on this issue?

Many thanks in advance,

Jamie[php][/php]

(Anthony Barnes) #2

This is generally caused by the data coming back from the request not being in JSON format. If possible are you able to use a tool like firebug and paste the response data from the getGeneral() call? My guess would be an error coming back in Matrix default error format and not as a JSON response.


(Jamie Smith) #3

Hi Anthony. I've checked and jsonToObj() is actually being passed an empty string. Can't figure out what I'm doing wrong.


Jamie


(Nic Hubbard) #4

[quote]
Hi Anthony. I've checked and jsonToObj() is actually being passed an empty string. Can't figure out what I'm doing wrong.



Jamie

[/quote]



In Firebug, do you see the ajax call being made? You should be able to see that response…at least that would be a start to figuring out what is going wrong.


(Jamie Smith) #5

Chrome is reporting a 200 OK status code for the XHR request.


The only other thing reported is that the JS API asset itself (javascript_api_1.js - not the one with the appended query string parameters sent as the XHR request) has been transferred with a MIME type of text/plain. Don't know whether that's indicative of incorrect server behaviour - I don't have a great knowledge of that side of things.



Jamie


(Anthony Barnes) #6

We do a fair bit of work with the JS API and I haven't had a whole lot of success debugging this without firebug in Firefox, there are other tools out there but this is by far the easiest. In the console you will see the POST requests and be able to inspect the actual response data coming back from the server.


The request will return a 200 response code regardless, since that's only checking that the server (apache) received the request and was able to respond. I think that in this case the data coming back from matrix will be empty and not the {error:""} JSON object you might be expecting if matrix wasn't successful in processing the JS API request. You might also try checking the matrix error logs and see if you can spot anything being generated there that isn't being returned by the JS API.


(Jamie Smith) #7

Thanks. Checked with Firebug and the data response is indeed empty. I also looked at the Matrix error logs and noted this when I viewed the page making the JS API request:


[font="Courier New"][color="#FF0000"][font="Times New Roman"]Raw Entry:[/font] [10-May-2010 16:09:47] PHP Fatal error: JS_Api::require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'Services/JSON.php' (include_path='.:/var/www/mysource_matrix/php_includes:/usr/share/pear:/usr/share/php') in /var/www/mysource_matrix/packages/web_services/js_api/js_api.inc on line 1398[/font]



Does this offer any clues as to what the issue is?


(Nic Hubbard) #8

[quote]
Thanks. Checked with Firebug and the data response is indeed empty. I also looked at the Matrix error logs and noted this when I viewed the page making the JS API request:



[font=“Courier New”][color="#FF0000"][font=“Times New Roman”]Raw Entry:[/font] [10-May-2010 16:09:47] PHP Fatal error: JS_Api::require_once() [<a href=‘function.require’>function.require</a>]: Failed opening required ‘Services/JSON.php’ (include_path=’.:/var/www/mysource_matrix/php_includes:/usr/share/pear:/usr/share/php’) in /var/www/mysource_matrix/packages/web_services/js_api/js_api.inc on line 1398[/font]



Does this offer any clues as to what the issue is?

[/quote]



Yup, you must not be running php 5.2.x, so you don’t have json support. You can either upgrade php, or use the Services_JSON pear package. Take a look at: http://matrix.squiz.net/resources/requirements


(Jamie Smith) #9

Thanks for your help, chaps. Will install the PEAR package.


Jamie