1

I am new with javascript, need help to post data to web service. I have simple page:

        <form action="#" class="simple-form">
            <input id="A" type="text" autocomplete="off">
            <input id="B" type="text" autocomplete="off">
            <input id="amount" type="text" autocomplete="off">
        </form>
        <button align="middle" class="button1" id="create" 
                           onclick="f1()">Create</button>

I need to get values of input A, B and amount, then pass them to url: "http://localhost:8080/dopayment/" with POST in xml format. The exact xml format must be:

<Payment>
    <a>xxx</a>
    <b>xxx</b>
    <amount>xx</amount>
</Payment>

P.S from Postman I have checked above given XML with post to given URL, it is working.

Thanks in advance to everyone.

1
  • Might take a look at - stackoverflow.com/questions/29705873/… Probably what you're looking for. You'll want a couple of libraries such as request (to make the http request), xml2js (to handle xml formatting). @yBodsky example would just require you to get the XML to the function itself. Commented May 14, 2019 at 15:05

1 Answer 1

0

Since you create the form, put button into it to trigger submit event.

 <form action="#" class="simple-form">
        <input id="A" type="text" autocomplete="off">
        <input id="B" type="text" autocomplete="off">
        <input id="amount" type="text" autocomplete="off">
        <input type="submit" align="middle" class="button1" value="Create">        
 </form>

You can sent info by AJAX, this way:

document.getElementsByClassName("simple-form")[0].addEventListener('submit', (e) => {
  e.preventDefault();
  const formData = new FormData(e.target);
  const data = `<Payment>
   <a>${formData.get('A')}</a>
   <b>${formData.get('B')}</b>
   <amount>${formData.get('amount')}</amount>
  </Payment>`;

  fetch('http://example.com', {
    method: 'POST', // or 'PUT'
    body: data, // data can be `string` or {object}!
    headers:{
      'Content-Type': 'application/xml'
    }
  }).then(res => res.json())
  .catch(error => console.error('Error:', error))
  .then(response => console.log('Success:', response));
});
Sign up to request clarification or add additional context in comments.

Comments

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.