从 dojo xhrPost 中提取文本

发布于 2024-12-07 20:49:15 字数 907 浏览 0 评论 0原文

我有一个正在执行 dojo.xhrPost() 的函数。现在,返回的数据被包装在不需要的

中,该
是特定于框架的,无法删除。我怎样才能去掉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 是否允许我操作类似于

包含我的文本
的请求数据

I have a function in which I am doing a dojo.xhrPost(). Now the returning data is wrapped in an unwanted <div> which is framework specific and cannot be removed. How can I strip away the div element. Here is my code.

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");
}

In jquery we would do $("#some_ID").text(); where the id will be the div obtained via ajax.
Will dojo allow me to manipulate the request data which is like <div id="unwanted_div">containing my text</div>

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魄砕の薆 2024-12-14 20:49:15

我不确定这些是“最佳”方法,但它们应该有效


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。

I am not sure these are the "best" ways to go at it but they shoud work


1) Have the data be interpreted as XML instead of plain text:

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) Convert it to html and then process it

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

2.1) Use dojo.query instead of .firstChild if you need smore sofistication.

烛影斜 2024-12-14 20:49:15

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

I prefer handle as JSON format :) , dojo have more utilities to access and to iterate the response.

 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";
            }
          }
      });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文