私は自分自身を JSONP に少し読み込んでいます...しかし、私はまだばかげているように感じます。次のページを作成しました。このページは、JSON 表記で Web サービスによって情報を取得する必要があります。
<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>
このトピックに関する Liams のコメントの助けを借りてこれを作成しました: Simple jQuery, PHP and JSONP example?
UPDATE 13-07-2012 15:06 サーブレットから正しい結果を得ることができましたが、受信 Web サイトから別のエラーが発生しました (「エラー: res が定義されていません」)。サイトが読み取れるように、JSONP データの正しいキーは何でしょうか?
一方、SPRING と Java サーブレットを使用する Webサービスがあり$.getJSON('http://localhost:8080/test/services/export/getuser.mvc?callback=?',...)
ます。このサービスは次のように呼び出されます。
@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;
}
}
残念ながら、アラートを受け取ることはありません。私は何を間違えましたか?
更新 このエラーメッセージは何を意味しますか:「res not found」はどういう意味ですか? 発見されるには、どのような名前が必要ですか?