i read myself a bit into JSONP... but it feels like I am still dumb. I created the following page, which should get the information by my webservice in a JSON notation:
<html>
<head>
<title> SomeHtml Form </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function getParameters(){
$.getJSON('http://localhost:8080/test/services/export/getuser.mvc?callback=?',function(res){
alert('Your name is '+res.lname);
});
}
</script>
</head>
<body >
<button onclick="getParameters();">Get Name</button>
</body>
</html>
I created this with the help of Liams Comment on this topic: Simple jQuery, PHP and JSONP example?
UPDATE 13-07-2012 15:06 I managed to get the right result from my Servlet, but now I get a different error from the receiving website ("Error: res is not defined"). What would be the right key, for my JSONP data, so the site can read it?
On the other hand I have a webservice using SPRING and Java Servlets, which gets called by $.getJSON('http://localhost:8080/test/services/export/getuser.mvc?callback=?',...)
This service looks like this:
@Controller
@RequestMapping("/services/export/")
public class AccountServiceProviderController extends ProfileDashboardController {
private static final String DESC_NAME = "ACCOUNT SERVICE PROVIDER CONTROLLER";
@RequestMapping("/getuser")
public @ResponseBody
JSONPObject list(final HttpServletResponse response) {
return new JSONPObject("res", getJson());
}
public JSONObject getJson() {
User user = getCurrentUser();
// logger.debug("ID: " + id);
// json
JSONObject json = new JSONObject();
json.put("lname", user.getLastName());
json.put("fname", user.getFirstName());
logger.info(json.toString());
return json;
}
}
Unfortunately I never get an alert. What did I do wrong?
UPDATE What does this error message: "res not found" mean. What does it has to name like to be found?