开发者

how to call cross domain WebService/WCF in JQuery

UPDATE1:

here is what i am getting when i copy and paste the service url in the IE browser:

http://myservername/myservices.svc?wsdl

- <wsdl:message name="ILodge_GetCountfor">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfore">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfor_Input">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>
- <wsdl:message name="ILodge_GetCountfor">
  <wsdl:part name="parameters" element="tns:GetCountfor" /> 
  </wsdl:message>

http://myservername/myservices.svc?xsd=xsd0

- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="GetCountforResult" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="Id" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="LevelId" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="GetCountfor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="GetCountfor" type="xs:long" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>

UPDATE:

i see the services is returning me as XML:

 public override string ToString()
    {            
        //- <name>CLUE</name><desc>CLUE list</desc> 
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        sb.AppendLine("<kml xmlns=\"someSITE">");
        sb.AppendLine("<FOLDER>");
        sb.AppendLine("<name>Clue</name>");
        sb.AppendLine("<desc>Clue list</desc>");
        sb.AppendLine("</FOLDER>");
        sb.AppendLine("</kml>");
        return sb.ToString();
    }

i have tried different way to execute the below cross domain reference but no success... what i am doing wrong here? i try debugging and put break point but looks like it never execute

 $(document).ready(function () {

$.getJSON("http://servername/tools/myservice.svc/mymethod/?Id=1&callback=?", null,           
      function (result) {
        alert("in test: " + result);
        debugger
        $("#spText").html(result);
    }); 

OR

        var path = "http://servername/tools/myservice.svc/mymethod?Id=1&callback=?";
             $.ajax({
                 type: "GET",
                 url: path,
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: false,
                 success: function (response) {
                    debugger
                     if (response != null) {
                         //displayData(response);
                     }
                 }
             });

OR
             debugger
             $.ajax({ url: "http://servername/tools/myservice.svc/mymethod",
                 data: { Id: "1" },
                 dataType: "jsonp",
                 success: function (json, textStatus) {
                     alert(json.d);
                     alert(textStatus);
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     debugger
                 }
             });


    OR
             $.ajax({
                 type: "GET",
                cache: false,
                url: "http://servername/tools/myservice.svc/mymethod/Id=1&callback=?",
                              scriptCharset: "utf-8",
                              dataType: "jsonp",
               开发者_如何学JAVA               data: parameters,
                              success: function (data, textStatus) {
                                  debugger
                              },
                              error: function (XMLHttpRequest, textStatus, errorThrown) {
                                  debugger
                              }
                          });
}


i have tried different way to execute the below cross domain reference but no success...

Browsers do not allow cross domain ajax requests due to the security risks. If your web page and web service are on different domains you will need to add a second page, on the same domain as your web page, to broker the requests.


I guess the "response" is not valid jsonp.

Copy the url into your browsers adressbar an show us what you'll see.

Here an example how jsonp looks like:

someFunction({'foo':'some foo','bar':'some bar'})

A live example from flickr: http://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=08e4f6fc4216b1216c5f521133ecbd9b&jsoncallback=functionName

It looks like the call of a function with an object-literal as argument. It looks like that, because it will later be a function-call. The way jsonp works is: A <script> -element will be injected into the DOM , which has set the provided URL as src-attribute. So the ressource is not a string or XML-document, it's a javascript-file. If it is embedded into the document it will be executed and the argument is accessible.

I can't tell you the ultimate way to create jsonp, it depends on where you get the data from and what you like to do with it(and of course on the given environment too).


The normal .Net extension for WCF services is .svc it looks like all of your calls are to .svs

I don't think .svs is a normal .net extension so the runtime is probably not even trying to execute the call server side.

Change the extensions or reconfigure your web server.


to be honest i don't know about asp.net , but i think you missed to escape this sb.AppendLine("<kml xmlns=\"someSITE">"); , it should be like : sb.AppendLine("<kml xmlns=\"someSITE\">");

you can tell it from the highlighting feature of SO editor , Thanks SO Team :)

   public override string ToString()
        {            
            //- <name>CLUE</name><desc>CLUE list</desc> 
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.AppendLine("<kml xmlns=\"someSITE\">");
            sb.AppendLine("<FOLDER>");
            sb.AppendLine("<name>Clue</name>");
            sb.AppendLine("<desc>Clue list</desc>");
            sb.AppendLine("</FOLDER>");
            sb.AppendLine("</kml>");
            return sb.ToString();
        }


The XMLHttpRequest object is strictly prohibited from calling web services outside of the domain that originally served the page. There are huge security reasons for this.

In your case the domain which served the page is "localhost". The javascript cannot access the remote webservice. You won't get around this.

That said, you do have an option. You can have your javascript hit a LOCAL web service which turns around and calls the REMOTE web service and passes the information back to your javascript. For more information see this article on Simple-Talk which goes into detail on several potential options available to you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜