最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Invoking WCF Service through Javascript - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 12, 2008 at 13:21 Robert Gamble 109k26 gold badges147 silver badges138 bronze badges asked Nov 12, 2008 at 13:11 VeeruVeeru
Add a ment  | 

2 Answers 2

Reset to default 4

By using XMLHTTP you ARE using ajax.

There's a full example here:

jQuery AJAX calls to a WCF REST Service

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?

发布评论

评论列表(0)

  1. 暂无评论