views:

3321

answers:

3

How can I access the WCF Service through JavaScript? My problem is, I have to access the operation contracts through the JavaScript (my website is not Ajax enabled).
Previously for calling .asmx web services, I am using the following code snippet

var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open("POST", URL, false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(payload);
xmlData = xmlHttp.responseXML;

where url is my webservice location.

Now if I am trying to consume the wcf service in the same manner, I am not able to. Many techies are explaining through AJAX approach, I need an approach without AJAX.

+1  A: 

By using XMLHTTP you ARE using ajax.

There's a full example here:

jQuery AJAX calls to a WCF REST Service

A: 

ThankQ for your reply.

Can't I use the same code to invoke the WCF Service

var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open("POST", URL, false);
xmlHttp.setRequestHeader("Content-Type",
      "application/x-www-form-urlencoded");xmlHttp.send(payload);
xmlData = xmlHttp.responseXML;

Previously while invoking the webservices the service url is like this Customers.asmx/GetCustomer?CustomerID=1794

Can i use the same approach to invoke the wcf service Customers.svc/GetCustomer?CustomerID=1794

i have gone throught the link that you have posted, but i should not use any third party files(json2.js) sorry for that.

If possilbe Please suggest me the changes in the above code to proceed further with wcf services

A: 

Look at the code on the link that I have sent before. Sure U can implement it yourself but it well be a huge effort duplication.

First, your WCF service must have:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

Then, on the javascript side, change the

"Content-Type", "application/x-www-form-urlencoded"

To

"Content-Type", "application/json"

Remember that the response will be json formated, so having a parser could be useful.

Why you don't want to use external libs?