Jump to content


Photo

JavaScript API - Uncaught SyntaxError: Unexpected token )


  • Please log in to reply
8 replies to this topic

#1 jsmith

jsmith

    Member

  • Members
  • PipPip
  • 95 posts

Posted 05 May 2010 - 02:02 AM

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" src="http://<domain>/_web_services/javascript_api_1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

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

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


Edited by jsmith, 05 May 2010 - 02:06 AM.


#2 Anthony Barnes

Anthony Barnes

    Implementation Specialist

  • Squiz Staff / Forum Moderator
  • 733 posts
  • Gender:Male

Posted 05 May 2010 - 09:57 AM

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.
-- Easy Edit Suite Download | Manual | Issues --

#3 jsmith

jsmith

    Member

  • Members
  • PipPip
  • 95 posts

Posted 05 May 2010 - 07:31 PM

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

Jamie

#4 Nic Hubbard

Nic Hubbard

    Advanced Member

  • Squiz Clients
  • PipPipPip
  • 5,119 posts
  • Gender:Male
  • Location:Washington State, USA

Posted 05 May 2010 - 11:42 PM

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

Jamie


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.

Nic Hubbard

Owner - Zed Said Studio (Squiz Matrix Freelance & Consulting)
Webmaster - Pacific Union College


#5 jsmith

jsmith

    Member

  • Members
  • PipPip
  • 95 posts

Posted 06 May 2010 - 02:32 AM

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

#6 Anthony Barnes

Anthony Barnes

    Implementation Specialist

  • Squiz Staff / Forum Moderator
  • 733 posts
  • Gender:Male

Posted 06 May 2010 - 10:47 AM

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.

Edited by Anthony Barnes, 06 May 2010 - 10:48 AM.

-- Easy Edit Suite Download | Manual | Issues --

#7 jsmith

jsmith

    Member

  • Members
  • PipPip
  • 95 posts

Posted 11 May 2010 - 01:24 AM

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:

Raw Entry: [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

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

#8 Nic Hubbard

Nic Hubbard

    Advanced Member

  • Squiz Clients
  • PipPipPip
  • 5,119 posts
  • Gender:Male
  • Location:Washington State, USA

Posted 11 May 2010 - 03:36 AM

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:

Raw Entry: [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

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


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....es/requirements

Edited by nnhubbard, 11 May 2010 - 03:37 AM.

Nic Hubbard

Owner - Zed Said Studio (Squiz Matrix Freelance & Consulting)
Webmaster - Pacific Union College


#9 jsmith

jsmith

    Member

  • Members
  • PipPip
  • 95 posts

Posted 11 May 2010 - 06:51 PM

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

Jamie




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users