tags:

views:

44

answers:

2

hi

I've a JSP page like this with name test.jsp

<%@ page language="java" contentType="text/json; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%
response.setContentType("application/x-javascript");
%>

{"feed": "test"}

and an html page where i use jquery for reading the json object.

    <html>
    <head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'&gt;&lt;/script&gt;
    <script type='text/javascript'>
    $(function(){

    $.getJSON("localhost:8080/test.jsp?callback=?",{test:"test"}, function(data){alert("here");});

    })

    </script>
    </head>
    </body>
    something here
    </body>
    </html>

but it shows an error as invalid label in firefox. Can anyone explain me the reason for this error. I've tried google but could not find a solution or explanation to my problem. What needs do be done for this. Please help me out. Thanks

A: 

I have encountered the same issue which was actually because of the some issue with json or ajax call. In your json call it should be {"test":"test"} instead of {test:"test"}.

Try it should work if this is the issue.

anand
i did that also but seems not the case for the problem. Same invalid error again. Anything else please if you can think of.
Shwetanka
A: 

I've found the solution to the problem by some hit & trial. It is because when we make getJSON() call on cross domain then response should be wrapped inside a function name. This function is our callback handler for the response in the script.eg

html file :

 <html>
    <head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'&gt;&lt;/script&gt;
    <script type='text/javascript'>
    $(function(){
       $.getJSON("localhost:8080/test.jsp?callback=?");
    }
   function test(data){
      alert(data.feed);
    }

And the jsp test.jsp

<%@ page language="java" contentType="text/json; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%
response.setContentType("application/x-javascript");
out.write("test({\"feed\": \"test\"})");
%>

hope if anybody has the same problemm this will help.you can pass the callback function name as a parameter and make jsp at the server such that it uses that name to wrap the response.

Shwetanka