从dojo xhrPost中提取文本
我有一个函数,其中我正在执行一个从dojo xhrPost中提取文本,dojo,xmlhttprequest,Dojo,Xmlhttprequest,我有一个函数,其中我正在执行一个dojo.xhrPost()。现在,返回的数据被包装在一个不需要的中,它是特定于框架的,无法删除。如何剥离div元素。这是我的密码 function sendForm() { var resultNode = dojo.create("li"); dojo.xhrPost({ url: "${sectionaddurl}", form: dojo.byId("sectionform"), load:
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是否允许我操作包含文本的请求数据,如
有什么想法吗 我不确定这些是“最好”的方法,但它们应该有效
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。我更喜欢以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";
}
}
});
我决定在ajax页面中使用JSP页面而不是JSPX页面,这样就没有不需要的div