【问题标题】:extract text from dojo xhrPost从 dojo xhrPost 中提取文本
【发布时间】:2011-10-03 20:24:43
【问题描述】:

我有一个函数,我正在执行dojo.xhrPost()。现在返回的数据被包裹在一个不需要的<div> 中,它是特定于框架的,不能被删除。我怎样才能去掉 div 元素。这是我的代码。

function sendForm() {
    var resultNode = dojo.create("li");
    dojo.xhrPost({
        url: "${sectionaddurl}",
        form: dojo.byId("sectionform"),
        load: function(newContent) {
            dojo.style(resultNode,"display","block");
            resultNode.innerHTML = newContent;                   
        },
        error: function() {
            resultNode.innerHTML = "Your form could not be sent.";
        }
    });           
    $("#sectionform")[0].reset();
    dojo.place(resultNode, "existing_coursesection", "first");
}

在 jquery 中,我们将执行$("#some_ID").text();,其中 id 将是通过 ajax 获得的 div。 dojo 是否允许我操作请求数据,例如 <div id="unwanted_div">containing my text</div>

有什么想法吗?

【问题讨论】:

    标签: dojo xmlhttprequest


    【解决方案1】:

    我不确定这些是“最好”的方法,但它们应该可以工作


    1) 将数据解释为 XML 而不是纯文本:

    dojo.require('dojox.xml.parser');
    
    dojo.xhrPost({
        //...
        handleAs: 'xml',
        //...
        load: function(response_div){
            //content should be xml now
            result.innerHTML = dojox.xml.parser.textContent(response_div);
        }
        //...
    })
    

    2) 转换成html,然后处理

    //create a thworwaway div with the respnse
    var d = dojo.create('div', {innerHTML: response});
    result.innerHTML = d.firstChild.innerHTML;
    

    2.1) 如果您需要更精致,请使用 dojo.query 而不是 .firstChild。

    【讨论】:

    • 我决定为我的 ajax 页面使用一个 JSP 页面,其中包含一个 JSPX 页面,这样就没有不需要的 div
    【解决方案2】:

    我更喜欢以 JSON 格式处理 :) ,dojo 有更多实用程序可以访问和迭代响应。

     dojo.xhrGet({
            url : url,
            handleAs : "json",
            failOk : true, //Indicates whether a request should be allowed to fail
            //(and therefore no console error message in the event of a failure)
            timeout : 20000,
            content: {//params},
            load: function(){ // something },
            preventCache: true,
            error: function(error, ioargs) {
                console.info("error function", ioargs);
                var message = "";
                console.info(ioargs.xhr.status, error);
                //error process
              },
            handle: function(response, ioargs) {
                var message = "";
                console.info(ioargs.xhr.status, error);
                switch (ioargs.xhr.status) {
                case 200:
                    message = "Good request.";
                    break;
                case 404:
                    message = "The page you requested was not found.";
                    break;
                case 0:
                    message = "A network error occurred. Check that you are connected to the internet.";
                    break;
                default:
                    message = "An unknown error occurred";
                }
              }
          });
    

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      相关资源
      最近更新 更多