1

I want to call my server asynchronously.

My code is as below:-

function GetSynchronousJSONResponse(url, postData) {
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) {
if (new ActiveXObject("Microsoft.XMLHTTP"))
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open("POST", url, false);
xmlhttp.send(postData);
var responseText = xmlhttp.responseText;
return responseText;
}

But service call gives "Bad Request" error. Please help

3
  • show you postData values Commented Oct 29, 2015 at 9:25
  • 3
    Why not use jQuery? It will save you a lot of trouble in the future.. Commented Oct 29, 2015 at 9:26
  • You did read the docs, didn't you? Commented Oct 29, 2015 at 10:14

2 Answers 2

1

You forgot to add content type in your request. please add below line and try again xmlhttp.setRequestHeader("Content-Type","application/json;charset=utf-8");

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

Comments

0

function GetSynchronousJSONResponse(URL,postData) {

        $.ajax({

           url : URL,

           type : "POST",

           data : JSON.stringify(postData),//if required

           contentType : 'application/json',

           success : function(data) {}
   })
}

you can Try this .....................

2 Comments

Don't call this function GetSynchronous…!
Yes, in the first line of your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.