我一直在尝试对休息服务进行$ ajax()调用。我需要使用基本身份验证来进行此调用,所以我将beforeSend处理程序添加到了我的ajax调用中。当我在Fiddler中跟踪这个时,我没有看到它将授权头附加到我的请求中。beforeSend不会将授权标头添加到我的jquery ajax调用(基本授权)
<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>
围绕这个建议会有很大的帮助。另外,有人可以评论一下,为什么fiddler将这个请求作为OPTIONS类型来追踪,当它应该是GET类型时?
此外,我得到一个405错误:方法不允许在小提琴上的回应。 – vaibinewbee 2013-02-13 18:31:40