我一直在尝试对休息服务进行 $ajax() 调用。我需要使用基本身份验证来进行此调用,因此我将 beforeSend 处理程序添加到我的 ajax 调用中。当我在 Fiddler 中跟踪它时,我没有看到它将授权标头附加到我的请求中。
<body>
<div id="Button1" style="border: 2px solid blue; font-family: Cambria;">
Get CMS Content
</div>
<br />
<div id="error" style="color: Red; font-family: Cambria; font-size: large; display: none;">
</div>
<span style="font-family: Cambria; color: #009933; font-weight: bold">Result:</span>
<div id="result">
</div>
<script src="scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function () {
$('#error').fadeOut('slow');
//var requestParams = { 'format': 'json', 'username': 'administrator', 'password': '' };
var requestParams = { 'format': 'json' };
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic YWRtaW5pc3RyYXRvcjo=");
},
url: "http://localhost/KenticoCMS7/rest/cms.country",
data: requestParams,
dataType: "text",
contentType: "application/json;",
success: function (result) {
$('#error').fadeOut('slow');
$('#result').text(result);
},
error: function (xhr, status, errorThrown) {
$('#error').text(JSON.stringify(xhr));
$('#error').fadeIn('slow');
}
});
});
});
</script>
围绕这个的建议会有很大帮助。另外,有人可以评论一下,为什么提琴手将此请求跟踪为 OPTIONS 类型,而它应该是 GET 类型?