Using jQuery to perform a GET request and using the resulting data
I have a page, let's call it "callme.html" which only has this content:
abc
Now I want to fire the following:
$.get("callme.html", function (data) {
alert(data);
}, "text");
I am using jQuery 1.4开发者_JAVA百科.2 mini and the page is called but the alert is empty.
Any ideas why? I'd like the popup to contain abc
I've also tried the following
$.ajax({
url: "callme.html",
async: false,
success: function (data) {
alert(data);
}
});
Use chrome's developer tool, or fire bug. This lets you see any errors, or where the request went, if it was successful, etc...
Your $.get()
call is fine. You need to wrap it so that it fires on load.
This works for me:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$.get("callme.html", function (data) {
alert(data);
}, "text");
});
</script>
I solved it with using jsonp instead and using pre-loaded images in javascript.
$.getJSON("www.mypage.com?callback=?",
function (data) {
requestid = data.guid;
});
When you provide callback=? it will be replaced by getJSON with the appropriet identifier for the callback function.
However, now I must have control over mypage.com, which I have. So problem solved!
精彩评论