开发者

call WCF method

I have WCF service

ITourService.cs

namespace Service
{
    [ServiceContract]
    public interface ITourService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]
        double?[] GetPoints(string tourname);
    }
}

Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="TourService.TourService">
        <endpoint binding="webHttpBinding" contract="TourService.ITourService" behaviorConfiguration="webHttp"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

So I used jQuery 1-6-2.min.js and tried to call a method.

Function Start() starts a call

var varType;
var varUrl;
var varData;
var varContentType;
var varDataType;
var varProcessData;

function CallService() {
                $.ajax({
                    type          : varType, //GET or POST or PUT or DELETE verb
                    url           : varUrl, // Location of the service
                    data          : varData, //Data sent to server
                    contentType   : varContentType, // content type sent to server
                    dataType      : varDataType, //Expected data format from server
                    processdata   : varProcessData, //True or False
                    success       : function(msg) {//On Successfull service call
                    ServiceSucceeded(msg);                    
                    },
                    error: ServiceFailed// When Service call fails
                });
        }

function Start() {
    varType = "POST";
    varUrl = "http://localhost:1592/TourService.svc/GetPoints/";
    varData = '{tourname:customname}'; //tourname=customname doesn't works too and many other variants
    varContentType = "application/json; charset=utf-8";
    varDataType = "xml";
    varProcessData = false; 
    CallService();
}

function ServiceSucceeded(result) {
    alert(result);
}

function ServiceFailed(result) {
    alert('Service call failed: ' + result.status + ' ' + result.statusText);
    varType = null;
    varUrl = null;
    varData = null;
    varContentType = null;
    varDataType = null;
    varProcessData = null;
}

However something going wrong.

I have a message: Service call failed: 0 error

WCF Service works fine, I checked it using standart client on C#

I think a mistake is in an access from javascript to WCF, but I don't know wher开发者_Python百科e.


You have implemented a web service according to W3C standards, i.e. your web service expects requests in XML and answers with responses in XML. In your Javascript code, you however create a REST request with JSON data.

When you monitor the communication between your browser and the service, you're likely to see that the service answers with "404 Not found" since it will only answer on the URL http://localhost:1592/TourService.svc and not on http://localhost:1592/TourService.svc/GetPoints/.

So to go forward, you'll need to turn your web service into a REST service. I also recommend to not only use JSON for requests, but for the responses as well.

You can find an example of a simple REST service built with WCF in this answer.


varData = '{"tourname": "customname"}';

should work.


I think just

varData = {tourname: "customname"};

should work;


You need processData to be true in order to convert the varData object to a encode string

varData = { tourname : 'customname' }
...
varProcessData = true; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜