I am working on an HTML page which is using jQuery's getJSON method to call another web application (different domain entirely).
The servlet in this other web app will get the request and return a JSON object indicating success or failure. So in my servlet I write to the output stream gotten from the response:
and then in my javascript which called the servlet:
The problem is that when I execute the JS I am seeing an error "Invalid label" in firebug.
I know jQuery works as I was able to make their example code run right out of the box. So I am thinking it must be something wrong with what I am writing to the output?
That can't possibly be your actual code as that won't compile. When posting it's important to show real code without mistakes, otherwise everyone's time, including your own, is wasted. Please read this for more information.
It's likely that your JSON is not properly formatted. But we need to see the real code before we can say for sure. [ July 24, 2008: Message edited by: Bear Bibeault ]
Please take more care when posting. Enough posts like that and people learn to avoid your questions.
Now, onto your issue. That error usually crops up when the JSON is malformed. Are you sure that you're setting it into the response correctly?
I set up the following test page:
where the file json.data contains:
and everything works fine.
Make sure that your response contains what you really think it does. The Firebug plugin for Firefox can show you exactly what is going on with your Ajax call, including the response body. [ July 25, 2008: Message edited by: Bear Bibeault ]
Was the end of the day and I was sloppy when I typed in the example code.
Also thanks for your response. I actually found the answer on my own and like you were saying it had to do with what I was returning.
Thought I would post the code from my servlet incase anyone else down the road runs into this like I did.
This is the doGet method from my test servlet which will handle the jQuery.getJSON request. It is making use of the JSONObject although it would be possible to just create the string on your own if you were so inclined.
Thanks again for the help :-) [ July 25, 2008: Message edited by: Seamus Minogue ]