从dojo xhrPost中提取文本

时间:2011-10-03 20:24:43

标签: dojo xmlhttprequest

我有一个功能,我正在做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>

这样的请求数据

任何想法?

2 个答案:

答案 0 :(得分: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)如果你需要smore sofistication,请使用dojo.query而不是.firstChild。

答案 1 :(得分:0)

我更喜欢以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";
            }
          }
      });