0
<script>
$.ajax({
    type: 'POST',
    url: '/view',
    data:'{"S":"Sam"}', 
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function(data) { alert('data: ' + data); }


});
</script>

When this script gets loaded I get a (400 Bad request). Since the data is starightforward, I need to know if there is anyway I can directly make this request to a URL, or what would be the easiest way to map it to my Spring controller so that I can read that data from the External URL?

Thanks

2
  • Can you also post the Java controller method that is supposed to capture this request? I once had a problem where I got a 400 error because a Java model object happened to not quite match the data being sent in the request. Commented Jun 27, 2014 at 14:55
  • I do not have a model for this, since the data has only 1 key/value pair. My question was a bit ambiguos I think. I meant, is there any way to capture this data to an external URL without having to define a model? Commented Jun 27, 2014 at 15:14

2 Answers 2

1

Can you try this way if not preferred direct way, BTW, i have not tested this code...

var myData = { name: value };
var request = $.ajax({
type: 'POST',
url: '/view',
data: myData, 
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false   
});

request.done(function(data){
   alert(data);
});
Sign up to request clarification or add additional context in comments.

Comments

0
<script>
$.ajax({
  type: 'POST',
  url: '/view',
  data: JSON.stringify({"S":"Sam"}),
  error: function(e) {
    console.log(e);
  },
  dataType: "json",
  contentType: "application/json"
});
</script>

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.