KOMU
KOMU

Reputation: 11

How to implement asynchronous service call in javascript?

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

Upvotes: 1

Views: 57

Answers (2)

Baqir
Baqir

Reputation: 717

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");

Upvotes: 1

sourav jena
sourav jena

Reputation: 1

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

Upvotes: 0

Related Questions