使用Javascript将xml发布到api [英] Using Javascript to POST xml to api
问题描述
我正在尝试发布XML数据,然后重定向到当前页面,但我似乎无法让它工作。
I'm trying to post XML data and then do a redirect to the current page, but I can't seem to get it to work.
当我这样做:
<form action="http://onehouse.freshdesk.com/helpdesk/tickets.xml" method="post">
它可以运行并将数据发布到我想要的网站。
it works and posts the data to the website that I want it to.
但是,它会引导用户访问XML页面,因此我想将其发布,然后将其重定向到我当前的联系表单页面并获得某种成功的消息。
However, it leads the user to an XML page so I want to post it and then have it redirect to my current contact form page and have a success message of some kind.
我尝试过使用:
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://onehouse.freshdesk.com/helpdesk/tickets.xml",true);
xmlhttp.send();
}
我的控制台没有出现任何错误,但它没有发布像我想要的那样到了freshdesk网站。有谁知道我做错了什么?
I don't get any errors in my console, but it does not post to the the freshdesk website like I want it to. Does anyone know what I'm doing wrong?
推荐答案
这是一个关于如何使用jQuery发布的例子:
This is an example on how to post with jQuery:
var data =
'<helpdesk_ticket>' +
'<description>This is a test</description>' +
'<email>sample@example.com</email>' +
'</helpdesk_ticket>'
$.support.cors = true;
$.ajax({
url: 'http://onehouse.freshdesk.com/helpdesk/tickets.xml',
type: 'POST',
crossDomain: true,
data: data,
dataType: 'text',
username: 'username1',
password: 'password1',
success: function (result) {
alert(result);
},
error: function (jqXHR, tranStatus, errorThrown) {
alert(
'Status: ' + jqXHR.status + ' ' + jqXHR.statusText + '. ' +
'Response: ' + jqXHR.responseText
);
}
});
这篇关于使用Javascript将xml发布到api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!