0

I have a jsp page, in which i have two buttons. Start and Stop buttons. When i click on start button calling one jquery, ajax function. But its getting error message.

Please check the code once

//$.support.cors = true;
$(document).ready(function() {
  jQuery.support.cors = true;

  url = "http://119.81.26.42/api/order";
  $("#start").click(function(e) {
    e.preventDefault();
    alert("jghjghjghj");
    var formData = {
      "GUID": "66f1a5ce-3f01-49c3-b825-e0a538ee4fb2",
      "Side": "SELL",
      "Currency": "USD",
      "Metal": "AU",
      "Qty": 0.005,
      "SHA1": "54745c22ac71adbf282a88c698322f26879124d1"
    }
    // var formData = $("#data").serialize();
    $.ajax({
      url: url,
      data: formData,
      dataType: "json",
      type: "POST",
      crossDomain: true,
      beforeSend: function(req) {
        req.setRequestHeader("Access-Control-Allow-Origin:", "*"),
          req.setRequestHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"),
          req.setRequestHeader("Access-Control-Allow-Headers", "X-ACCESS_TOKEN, Access-Control-Allow-Origin, Authorization, Origin, x-requested-with, Content-Type, Content-Range, Content-Disposition, Content-Description")

      },
      success: function(data) {
        alert(data);
      },
      error: function(jqxhr, status, errorMsg) {
        alert(status + ": :::::::::===" + errorMsg);
      }
    });
  });
});
table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <title>Trade Response</title>
</head>

<body>
  <form>
    <button name="button" id="start">Start</button>
    <button name="button" style="margin-left: 294px;" id="stop">Stop</button>
    <input type="text" name="data" value="" id="data" style="display: none;">
    <br>
    <br>
  </form>
</body>

</html>

Getting this error:enter image description here

1
  • 1
    Can you edit the code in the question using the "tidy" button? Commented Jun 15, 2017 at 10:21

1 Answer 1

2

The headers you are trying to set should come as a part of response and not set in request.

More over there are some specific header that you can't set. for example User-Agent, Content-Type.

Sign up to request clarification or add additional context in comments.

1 Comment

Content-Type is a valid request header (when applicable)

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.