JQueryを使用するSpring MVC Showcaseを勉強していますが、JQueryの世界は初めてです
この例を研究して確立された JQuery 関数の動作について、私は疑問を持っています。
したがって、「/mapping/produces」フォルダーへの HTTP リクエストを生成する次のリンクがあります。
<li>
<a id="byProducesAcceptJson" class="writeJsonLink" href="<c:url value="/mapping/produces" />">By produces via Accept=application/json</a>
</li>
ご覧のとおり、このリンクには「writeJsonLink」という名前のクラスがあり、このクラスには、リンクのクリックでトリガーされる次の JQuery 関数が定義されています。
$("a.writeJsonLink").click(function() {
var link = $(this); // Variable that contain the referer to the clicked link
// Execute ajax call
$.ajax({ url: this.href,
// Before send the request to the server execute the following function
beforeSend: function(req) {
if (!this.url.match(/\.json$/)) {
req.setRequestHeader("Accept", "application/json");
}
},
success: function(json) {
MvcUtil.showSuccessResponse(JSON.stringify(json), link);
},
error: function(xhr) {
MvcUtil.showErrorResponse(xhr.responseText, link);
}});
return false;
});
私の問題は、if条件の意味を理解するのに問題があることです.コードのこの部分はどういう意味ですか?
if (!this.url.match(/\.json$/)) {
req.setRequestHeader("Accept", "application/json");
}
HTTPリクエストにヘッダーを追加するだけのように見えるのですが、実際にはJSONで何かをしていると思います... ifロジック条件の意味を理解するのにも問題があります...
どうもありがとうアンドレア