[英]Why isn't string getting passed into function - jQuery
First, I grab HTML using jQuery's $.ajax function. 首先,我使用jQuery的$ .ajax函数获取HTML。
In the success function I alert it (this works correctly and shows an HTML string as expected); 在成功功能中,我发出了警报(它可以正常工作并按预期显示HTML字符串);
Then I pass it in as a variable to another function, processHTML. 然后,将其作为变量传递给另一个函数processHTML。 But it's not working and says the variable is null. 但是它不起作用,并说该变量为空。 I confirm this with an alert. 我会以警报确认。
var my = my || {};
jQuery(function ($) {
my.html = {
getHTML: function(url) {
$.ajax({
url: url,
dataType: "html",
success: function( myHTML ) {
alert(myHTML); // shows string, as expected
my.html.processHTML(myHTML); //returns null
}
});
},
processHTML: function ( myHTML ) {
alert(myHTML);
// do stuff and return myHTML
}
} // end of my.html object
}); // end of jQuery wrapper function
Why isn't the string getting passed from the success callback into the processHTML function? 为什么字符串没有从成功回调传递到processHTML函数中? If I replace myHTML in the success callback with an actual string ( <div>test</div>
), that gets successfully passed into the function. 如果我将成功回调中的myHTML替换为实际字符串( <div>test</div>
),则会成功将其传递到函数中。
Update: Here's the actual code, as requested. 更新:这是根据要求的实际代码。 It is invoked by clicking a link with onclick="hey.mydoc.ajax2({source: 'http://www.mysite.com/mypage'})"
. 通过单击带有onclick="hey.mydoc.ajax2({source: 'http://www.mysite.com/mypage'})"
的链接来调用它。 This is also on JSFiddle but I get ReferenceError: Can't find variable: hey
when clicking the link, which doesn't happen on my site. 这也在JSFiddle上,但是我得到ReferenceError: Can't find variable: hey
当单击链接时,这在我的网站上没有发生。
var hey = hey || {};
jQuery(function ($) {
hey.mydoc = {
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
getHTML: function ( source ) {
$.ajax({
url: source,
dataType: "html",
beforeSend: function(){
},
success: function( myHTML ) {
alert('myHTML is '+myHTML );
hey.mydoc.processHTML( myHTML );
} // end of success function
});
}, // end of method
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
processHTML: function ( myHTML ) {
alert ('processHTML - ' + myHTML );
myHTML = $(myHTML);
myHTML.find('script').remove();
// and a bunch of other DOM manipulations...
var content = "<!DOCTYPE html><html>" + myHTML.html() + "</html>";
return content;
}, // end of method
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
ajax2: function ( options ){
$.ajax({
url: options.content,
dataType: "html",
success: function( myHTML ) {
alert('myHTML is '+myHTML ); // myHTML is string of html, as expected
var newHTML = hey.mydoc.processHTML( myHTML ); // myHTML not getting passed in
alert(newHTML);
} // end of success function
});
} // end of method
} // end of hey.mydoc namespace
}); //end of jQuery wrapper
try it like this: 尝试这样:
function processHTML(myHTML) {
alert(myHTML);
// do stuff and return myHTML
}
I'm not sure but I think a friend of mine had the same problem last weekend. 我不确定,但我认为我的一个朋友上周末遇到了同样的问题。
I tolled him he'd better use deferreds http://api.jquery.com/category/deferred-object/ 我向他收费,他最好使用递延http://api.jquery.com/category/deferred-object/
$.ajax({ object literal except success attribute }).success(function() {..
Don't need to pass the parameter. 不需要传递参数。
$.ajax({
url: "/something/some.do",
type: "POST",
dataType: "html",
data: soapEnv,
complete: completeCallback,
contentType: "text/html; charset=\"utf-8\""
});
}
function completeCallback(myHTML) {
alert(myHTML)
}
试试看-在processHTML之后删除'='(等号)并放一个冒号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.