开发者

Call WCF Service Through Javascript, AJAX, or JQuery

开发者 https://www.devze.com 2023-02-04 03:51 出处:网络
I created a number of standard WCF Services (Service Contract and Host (svc) are in separate assemblies).I fired up a Web Site in IIS to host the Services (i.e., address is http://services:1000/wcfser

I created a number of standard WCF Services (Service Contract and Host (svc) are in separate assemblies). I fired up a Web Site in IIS to host the Services (i.e., address is http://services:1000/wcfservices.svc).

Then in my Web Site project I added the reference. I am able to call the services normally. I am needed to call some of the services client side. Not sure if I should be looking at articles calling WCF services through AJAX, JQuery, or JSON enabled WCF Services. Can anyone provide any thoughts or experience with configuring as such?

Some of the changes I made was adding the following to the Operation Contract:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "SetFoo")]
void SetFoo(string Id);

Then this above the implementation of the interface:

[AspNetCompatibilityRequirements(RequirementsMode = 
                           开发者_如何学Python AspNetCompatibilityRequirementsMode.Allowed)]

Then in the service webconfig I have this (parens are angle brackets):

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
  <baseAddressPrefixFilters>
    <add prefix="http://services:1000/wcfservices.svc/"/>>
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />

Then in the client side I attempted this:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
    <compositeScript>
        <Scripts>
            <asp:ScriptReference 
                      Path="http://Flixsit:1000/FlixsitWebServices.svc" />
        </Scripts>
    </CompositeScript>
</asp:ScriptManagerProxy>

I am attempting to call the service like this in javascript:

wcfservices.SetFoo(string Id);  

Nothing is working. If it is idea or a better solution to call JSON enable, JQuery, etc....I am willing to make any changes.

Thanks for any suggestions/tips provided....


The same origin policy will prevent the client from making AJAX calls to a service located in a different domain (different host) than the one serving up the web page. You can make this work using JSONP rather than JSON. You'll need to change your service to accept a callback function and deliver to the client a bit of javascript invoking this callback with the JSON data.

0

精彩评论

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