index.html
<html>
<head>
<script src="//code.jquery.com/jquery-2.0.0.min.js"></script>
<script src="jquery.js"></script>
</head>
<body>
<div id="myDiv" name="myDiv" title="Example Div Element">
</div>
</body>
</html>
jquery.js
$.ajax
({
type: "GET",
url: "url",
dataType: 'image/png',
async: false,
beforeSend: function (xhr) {
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", "Bearer xxx");
},
complete: function (data) {
console.log("yello");
$('#myDiv').html('<img id="target">');
}
});
问题很简单,
为什么这条线不起作用?$('#myDiv').html('<img id="target">');
当我检查页面的源代码时,什么也没有显示,没有图像标签。我不太清楚为什么会做正确的事情。
谢谢!
将函数包装在.ready()中:
$(function()
{
$.ajax
({
type: "GET",
url: "url",
dataType: 'image/png',
async: false,
beforeSend: function (xhr) {
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", "Bearer xxx");
},
complete: function (data) {
console.log("yello");
$('#myDiv').html('<img id="target">');
}
});
});
这确保在加载DOM之后调用ajax方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句