0

The alert with "success" is not fired when i run this. I checked with firebug, and it tells me that ret is undefined. What does that mean?

function checkUni() {

var URL = "http://localhost:8080/GradSchoolApp/test.jsp";

var ret = $.getJSON(URL, function(data, textStatus) {
    alert("success");
});
}

EDIT:

this is the test.jsp btw

<%@ page language="java" contentType="application/json; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*, net.sf.json.JSONObject"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("application/json");
JSONObject jo = new JSONObject();
jo.put("location", "1");
jo.put("name", "someUni");
out.println(jo);
out.flush();

%>
</body>
</html>
stianlp
  • 821
  • 9
  • 19
  • ret should not be undefined but a jqXHR object after the function is run - regardless whether success fires or not. Are you sure? – Bergi May 30 '12 at 04:35
  • yeah. as i wrote in another comment, i added one more line, and firebug showed be the return value. my problem is that the alert never fires so i am never getting into the function with 'data'... – stianlp May 30 '12 at 04:38
  • Maybe you're getting an error??? you should show us the error message if you want us to find the problem. – Bergi May 30 '12 at 04:46
  • When I replace my code with the one Jason Goemaat posted, I get 'success'. But that function is probably doing something else. I am not very good at this syntax at all, so I dont really know how to get print an error message. I'll see what I can do. – stianlp May 30 '12 at 04:57
  • I can print that .error(function() {alert("error");}); but i dont know which parameter to use to print the error message... – stianlp May 30 '12 at 05:03
  • I think I got the error: error: parsererror : SyntaxError: JSON.parse: unexpected character – stianlp May 30 '12 at 05:11
  • Ah, there it is. As in the comments to ChinBoon's answer came up, you should not serve HTML where JSON is needed :-) – Bergi May 30 '12 at 06:02

3 Answers3

1

The AJAX call did not work. A few points to check, is the URL correct? Did you check the application server log to see if the call has ever reached. Can you check firebug for the HTTP code if the AJAX call is successful?

EDIT:

(Changed from text/json to application/json) Thanks Bergi

JSP:

<%@ page contentType="application/json" %>

Java Servlet:

response.setContentType("application/json");
Oh Chin Boon
  • 21,393
  • 45
  • 133
  • 208
  • Ok. I put in another line with var s = "test"; and then i can see that ret is defined, and it has the responseText with both html and the json object text. But I want to use the data, isn't that supposed to be the json object? – stianlp May 30 '12 at 04:12
  • Can you please show the responseText. Yes it is supposed to be a JSON object (according to the JQuery API getJSON()..), but is the Java counterpart method also emitting the same response? i.e. JSON also – Oh Chin Boon May 30 '12 at 05:25
  • I printed ret.responseText and it looks like this: " Insert title here {"location":"1","name":"Air University"} " – stianlp May 30 '12 at 05:32
  • is this the same as my data parameter? If yes, is it a problem with my test.jsp? – stianlp May 30 '12 at 05:33
  • 1
    Yes i think the problem is with your JSP, i think a valid JSON response would look just like this: "{"location":"1","name":"Air University"}" If you are using Struts, you would need to set the response type to JSON, instead of HTML as per what you provided. – Oh Chin Boon May 30 '12 at 05:36
  • This is very helpful, cause I had a feeling that this was the problem - I've seen this html crap before... Hmm, no not using any framework yet. Do you have any idea where I can set this response type? – stianlp May 30 '12 at 05:37
  • For a Servlet, it would be "response.setContentType("text/json");" I have not tried on JSP before, but can you try this if you get anything? "" – Oh Chin Boon May 30 '12 at 05:39
  • Yeah, that might be the solution! Thank you! – stianlp May 30 '12 at 05:51
  • @ChinBoon: The [correct MIME type](http://stackoverflow.com/q/477816/1048572) is "application/json". – Bergi May 30 '12 at 06:00
  • The weird thing here is that when i run test.jsp in eclipse it prints a lot of html and stuff when i set the content type to text/json or application/json, when I leave it to text/html it prints pure json object. Its like the other way around...? – stianlp May 30 '12 at 06:05
  • @Bergi Thanks, i have modified it. user1424575, i don't get what you mean here. – Oh Chin Boon May 30 '12 at 06:14
  • @user1424575: You will need to remove the code that prints the HTML somewhere. The behaviour of eclipse is not weird: text/html will be rendered as a beatiful html document, while when delivering the same contents as json it will view the plain text file. – Bergi May 30 '12 at 06:23
  • What I mean is that if i leave it as it was with 'text/html' and open test.jsp in a browser, then it prints {"location":"1","name":"someUni"} and no more. if i change it to 'application/json' then it prints a lot of html also. – stianlp May 30 '12 at 06:25
  • and when in the firebug i can see that the responsetext is all that html in both cases. like this: Insert title here {"location":"1","name":"someUni"} " – stianlp May 30 '12 at 06:29
  • i put in test.jsp so you can see it – stianlp May 30 '12 at 06:33
  • OK. So first NOW i got it... I removed all the html from test.jsp, and now it works. I didn't think that was necessary, cause when opened in a browser it does not print the html code (ofcourse). Thanks guys - took me a while to get that... – stianlp May 30 '12 at 06:40
0

ret is scoped to your function, so only code inside your function can access it. My guess is that you don't have a web server running on port 8080 of your local computer or the URL is invalid for some other reason. Try .ajax if you want to get a callback on the error also (fiddle):

function checkUni() {
    var URL = "http://localhost:8080/GradSchoolApp/test.jsp";

    var ret = $.ajax(
    {
        url: URL,
        success: function(data, textStatus) {
            alert('success!');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('Error "' + textStatus + '": ' + errorThrown);
        }
    });
}

You might want to check your json too, the getJson page warns that it will fail silently on inavlid json, including strict requirements like quoted property names, you can try just using .get and deserializing yourself.

Jason Goemaat
  • 27,053
  • 14
  • 78
  • 109
  • Ok, that is weird. So the alert is not fired and I have no idea why. I have looked at alot of examples and the jQuery documentation, but I just dont get it. Can you give me a simple explanation how this is supposed to work? – stianlp May 30 '12 at 04:05
  • When I replace my code with this. It prints success, but 'data' is full of html and not only the json object. – stianlp May 30 '12 at 04:36
  • of course you won't be allowed to access the localhost from jsfiddle.net (same origin policy). – Bergi May 30 '12 at 04:37
  • yeah, removed that comment. look at the 2nd one. – stianlp May 30 '12 at 04:39
0

First check for ".jsp" file returning json data or not

Use one more parameter for callback

getJSON: function( url, data, callback ) {
    return jQuery.get( url, data, callback, "json" );
}

User like this

function checkUni() {
    var URL = "http://localhost:8080/GradSchoolApp/test.jsp";
    var ret = $.getJSON(URL,"",function(data){
        alert("success");
    });
}
Ranganadh Paramkusam
  • 3,768
  • 2
  • 21
  • 32
  • When I add that first piece of code, the whole file with the script disappear from 'scripts' in firebug... – stianlp May 30 '12 at 04:27
  • when I open the test.jsp in a browser i get: {"location":"1","name":"Air University"} that is what i am supposed to get, right? – stianlp May 30 '12 at 05:05
  • You'll get that in data object which i gave in the above snippet – Ranganadh Paramkusam May 30 '12 at 05:08
  • I am not following. What object? I'm sorry, but am not cool with the syntax yet i guess... – stianlp May 30 '12 at 05:16
  • Check this Fiddle example, you'll understand that http://jsfiddle.net/ranganadh/JUCe6/ – Ranganadh Paramkusam May 30 '12 at 05:21
  • ok, if I understand you correct, I ran the get function and printet out the data that was loaded (like one of the examples in the jQuery documentation). The loaded data was a lot of html with the json object in there. I beleive that this is my problem, cause I get a syntax error like this: 'error: parsererror : SyntaxError: JSON.parse: unexpected character' – stianlp May 30 '12 at 05:22
  • Where are you using JSON.parse? – Ranganadh Paramkusam May 30 '12 at 05:27
  • Once open that link which i sent you and open console to check the json object is coming or not! Where are you getting that parse error, then? – Ranganadh Paramkusam May 30 '12 at 06:20
  • if i put in something like this in my code: .error(function(jqXHR, textStatus, errorThrown) { alert("error: " + textStatus + " : " + errorThrown); }); – stianlp May 30 '12 at 06:22