views:

827

answers:

2

I have a bunch of Ajax requests that execute just fine, but I end up with a syntax error in my browser.

Can anyone see what's wrong with my request below?

function getName(refId)
{
    var resp = '';
    new Ajax.Request('/servlet/GetName',
    {
        method:'post',
        parameters: {'requestType':'ref',
           'value':refId},
     onSuccess:function(transport)
     {
      setName((String(transport.responseText)).trim());
     }
    });
}

edit: This also seems to only throw the error the first time it gets called. If I clear the console, I don't continue to get errors.

A: 

There is no root-property in the window object, according to W3Schools. Maybe if you use

new Ajax.Request(document.domain + '/servlet/GetName', ...

it will work better.

Edit, ah - I should learn to read. It does work, but throws a syntax error. Syntax errors != no implemented property... Sorry! But I will let my post stay anyway. ;)

Björn
As it turns out, window.root was returning nothing and it was implicitly adding the 'http://localhost:8080' to my request. Changing it to document.domain changed the request url so that it didn't work anymore. Hard-coding the url made no difference.
Jesse
+2  A: 

I've gotten syntax errors using xmlhttprequest in Firefox to query non-xml as well. Firefox tries to parse the response as xml, and logs the error, but keeps working anyways.

In my case, I added this to the php on the server side:
header('Content-Type: text/plain');

Someone reported a client-side fix here:
http://stackoverflow.com/questions/335409/jquery-getjson-firefox-3-syntax-error-undefined

David
I finally got around to fixing this, and setting the content type in the header worked! Thanks so much.I added this line to my servlet: response.setContentType("text/plain");
Jesse