Ajax后处理返回的数据

时间:2013-05-24 13:54:42

标签: jquery ajax post

我正在尝试代理一个函数,以便我只是替换部分页面而不是重新加载整个页面。

<div id="content" style="display:none;">
</div>
<div class='contentitem' id='restart' style="margin-top: 100px; margin-left: 100px; margin-bottom: 100px; display:block;">
    <table>
        <tr>
            <td></td>
            <td><p style="font-size: 1.2em; font-weight: bold;"><%=translate([==[Loading...]==])%></p></td>
        </tr>
    </table>
</div>

<script type="text/javascript">
//jquery library 
// released under BSD license
</script>

<script type="text/javascript" src="/jquery-1.9.1.min.js"></script>

<script type="text/javascript">
    var content = $("#content");

    $(document).ready(function(){   
        content.load('/cgi/b/sfltr/cfg/ div .contentitem', function(content) {
        $(this).find('tbody').each(function(){
              $("span:contains('Content Based Filtering')").closest("table").remove();
          });
        document.getElementById("restart").style.display = "none";
        document.getElementById("content").style.display = "block";
        //alert('Load was performed.');
    });
    });

  //intelligent version of submitform: will only refresh what is needed:
   (function() {  
   // proxying...
   var proxied = submitForm; // keep a copy of original method
   submitForm = function(theForm, actionNo, flags, primkey, confirmMsg, anchor, eval_code, customP) {
   //potential issue with the switch language... I think actionNo = 0 means reload.
   //alert('gotcha');
   // the following code is identical to what is in util.js
   //customP is only for restore page to upload config file to GW
    var VALIDATE = 0x01;
    var CONFIRM  = 0x02;
    var EVAL     = 0x04;

    if (flags && (flags & VALIDATE))
      if (!validate(theForm, actionNo)) return false;
    if (flags && (flags & CONFIRM))
      if (!confirm(confirmMsg)) return false;
    if (flags && (flags & EVAL) && eval_code)
      eval(eval_code);
    if (actionNo > 0)
      theForm.elements[0].value = actionNo;
    if (primkey)
      theForm.elements[1].value = primkey;
    var customArgs = new String("");
    if (g_state[TYPE].length > 0)
    {
      customArgs += "name="+encodeURIComponent(g_state[NAME]);
      if (g_state[KEY].length > 0)
        customArgs += "&" + g_urlArgs[KEY] + "=" + encodeURIComponent(g_state[KEY]);
    }
    if (g_state[TID].length > 0)
    {
      if (customArgs != "")
        customArgs += "&";
      customArgs += g_urlArgs[TID] + "=" + encodeURIComponent(g_state[TID]);
    }
    theForm.action += constructArgString(customArgs);
    if (customP)
      theForm.action += "&" + customP;
    if (anchor)
      theForm.action += "#" + anchor;
    //end of copy paste...


    //theForm.submit();
    //disable_fields(theForm);
        document.getElementById("restart").style.display = "block";
        document.getElementById("content").style.display = "none";

    //Now, I change the submit
    //theForm.submit(function () {

        $.ajax({
            type: $(theForm).attr('method'),
            url: $(theForm).attr('action'),
            data: $(theForm).serialize(),
          datatype: "html"
            })
        .done(function (html) {
            //alert(html);
            var Test = -1;
            $("html").find('p').each(function(){
            //var result = $("this").find(":contains('You are being redirected')"); //check if tries to redirect
            Test = Test?Test:this.indexOf("You are being redirected"); //if we already found we skip.
            //TBD portuguese variant...
            alert(this);
            alert(Test);
            });
            if (Test==-1) {
                content.html(html);
            content.find('table').each(function(){
                $("span:contains('Content Based Filtering')").closest("table").remove();
                // Filtragem com base em conteúdo
                $("span:contains('Filtragem com base em conte')").closest("table").remove();
                $(document).find("#restart").css("display","none");
                content.css("display", "block");
            });
            }
            else
            {
             GoAndRemember('/PC_ov.lp', '');
            }
            })
        .fail(function (xhr, textStatus, errorThrown) {
        alert('Unknown error:'+xhr.responseText);
        window.location.reload();
        });
  }
   return false;
   })();

</script>

当我使用该功能提交表单时,帖子有效,200OK包含我要解析的页面。

我不明白的是:

  1. 为什么我开始加载嵌入式脚本(嵌入在200OK中的代码脚本), =&GT;可能是因为我在将其过滤之前将其设置在div中,但我认为这是实现它的唯一方法。我应该创建一个新的XHR对象来存储它,然后过滤该对象并使用生成的.html()设置DIV吗?如果是这样,有人能给出一个小例子吗?
  2. 为什么我最终会收到错误,因为它会覆盖我正在使用的JavaScript(无论如何都应该是完全相同的代码)
  3. 由于

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案。似乎在使用数据类型“html”时,它将始终执行嵌入的js。这解决了这个问题: prevent jquery ajax to execute javascript from script or html response

错误是因为嵌入式JS工作所需的一些项目在此上下文中不可用。

非常感谢。