为什么没有字符串传递给函数 - jQuery

时间:2012-01-31 20:31:40

标签: javascript jquery

首先,我使用jQuery的$ .ajax函数获取HTML。

在成功函数中,我发出警报(这可正常工作,并按预期显示HTML字符串);

然后我将其作为变量传递给另一个函数processHTML。但它没有用,并说该变量为空。我通过提醒确认了这一点。

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

为什么字符串不会从成功回调传递到processHTML函数?如果我使用实际字符串(<div>test</div>)替换成功回调中的myHTML,则会成功传递给函数。


更新:根据要求,这是实际代码。通过单击onclick="hey.mydoc.ajax2({source: 'http://www.mysite.com/mypage'})"的链接来调用它。这也在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

4 个答案:

答案 0 :(得分:0)

尝试这样:

function processHTML(myHTML) {
    alert(myHTML);
    // do stuff and return myHTML
}

答案 1 :(得分:0)

我不确定,但我认为我的一位朋友上周末遇到了同样的问题。

我告诉他他最好使用延期http://api.jquery.com/category/deferred-object/

$.ajax({ object literal except success attribute }).success(function() {..

答案 2 :(得分:0)

不需要传递参数。

$.ajax({
        url: "/something/some.do",
        type: "POST",
        dataType: "html",
        data: soapEnv,
        complete: completeCallback,
        contentType: "text/html; charset=\"utf-8\""
    });
}

function completeCallback(myHTML) {
    alert(myHTML)
}

答案 3 :(得分:-1)

试试这个 - 在processHTML之后删除'='(等号)并放一个冒号。