0

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'></script>
    <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

Shwetanka
  • 4,734
  • 8
  • 40
  • 65
  • Why are you setting 2 different content types, Use `application/json` in accordance with `RFC 4627` - http://stackoverflow.com/questions/477816/the-right-json-content-type – RobertPitt Sep 28 '10 at 11:51
  • Sorry about that.It was a mistake. Nevertheless application/x-javascript is set at last. Both these work fine. – Shwetanka Sep 29 '10 at 11:32

2 Answers2

1

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'></script>
    <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
  • 4,734
  • 8
  • 40
  • 65
0

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.

Ankit Jaiswal
  • 21,029
  • 5
  • 38
  • 62
  • 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 Sep 24 '10 at 12:25