3

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.

When I do this:

<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.

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.

I've tried using:

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

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?

1
  • Take the data from the form and send it in the request. Commented Jul 31, 2013 at 17:24

2 Answers 2

1

This is an example on how to post with jQuery:

var data = 
    '<helpdesk_ticket>' +
    '<description>This is a test</description>' +
    '<email>[email protected]</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
        );
    }
});
Sign up to request clarification or add additional context in comments.

15 Comments

I just tested my script on a different domain and it works (jsfiddle.net/U47Nw) onehouse.freshdesk.com does not allow Cross-Domain requests
thanks for the reply but i get this error message when trying to implement that code XMLHttpRequest cannot load onehouse.freshdesk.com/helpdesk/tickets.xml. Origin localhost:3000 is not allowed by Access-Control-Allow-Origin.
@RickyAhn onehouse.freshdesk.com does not allow you to post data to their page because they do not have the "Access-Control-Allow-Origin: *" header set. Reference: en.wikipedia.org/wiki/Cross-origin_resource_sharing
do you think somewhere in the code i have to add the api key and password? would that be the issue?
@RickyAhn I cannot see the header in the Response. It could be that we are getting the 406 error response because of missing parameters and the header is not being added. You should try to set the required parameters in the request and see if it accepts a CORS POST.
|
0

As you tagged jQuery, here's a jQuery solution which should work (I did not test it):

$.post("http://onehouse.freshdesk.com/helpdesk/tickets.xml", {
   // any parameters here 
}, function(data) {
      document.getElementById("myDiv").innerHTML=data;
})

You still have to specify the parameters, but as you did not provide your form html, I left it empty. Also have a look at the api: jQuery.post

2 Comments

thanks for the reply when i tried it out i got this error in the console XMLHttpRequest cannot load onehouse.freshdesk.com/helpdesk/tickets.xml. Origin localhost:3000 is not allowed by Access-Control-Allow-Origin.
Yeah, I did not think about the cross domain .. try @MarcoBettiolo's solution, which does exactly the same, but should work for cross-site requests. Or you upload it, then it might work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.