是否可以使用 jQuery 方法 getJSON 将复杂对象跨域传递到 WCF 服务?

发布于 2025-01-06 08:04:08 字数 3653 浏览 0 评论 0原文

我正在尝试使用 jQuery 和 WCF 的组合在域之间传递信息。这对于传递简单数据类型非常有效。下面的 TestSimple 方法对此进行了演示。但是,当我使用 TestComplex 方法时,WCF 方法不会收到任何地址参数。

希望一个例子能让我的问题更清楚:

WCF 服务:

 Function TestSimple(postcode As String) As BasicAddress Implements ISubmitVehicle.TestSimple
      Return Test(postcode)
 End Function

Function TestComplex(Address As BasicAddress) As BasicAddress Implements ISubmitVehicle.TestComplex
    If Address Is Nothing Then
        Return Test("Nothing")
    Else
        Return Test(Address.Postcode)
    End If
End Function

Function Test(postcode As String) As BasicAddress
    Return New BasicAddress With {
        .Building = "Test", .County = "Test", .Latitude = 0, .Locality = "Test",
        .Longitude = 0, .Street = "Test", .Town = "Test", .Postcode = postcode}
End Function

客户端脚本

function TestSimple() {
     var data = { postcode: "test" };
     Test(data, 'TestSimple');
}

function TestComplex() {
     // I also tried
     //var data = { Address: { Postcode: 'test' } }
     var data = { 
          Address: { "__type": "BasicAddress:#WPC.FADS.Web",
              "Building": "Test",
              "County": "Test",
              "Latitude": 0,
              "Locality": "Test",
              "Longitude": 0,
              "Postcode": "test",
              "Street": "Test",
              "Town": "Test"
          }
     }
     Test(data, 'TestComplex');
}

function Test(data, method) {
    $.getJSON(baseURL + 'Services/SubmitVehicle.svc/' + method + '?callback=?', data,
    function (NewBasicAddress) { alert(NewBasicAddress.Postcode); })
}

数据契约:

<DataContract()>
Public Class BasicAddress
     <DataMember()> Property Building As String 
     <DataMember()> Property Street As String 
     <DataMember()> Property Locality As String 
     <DataMember()> Property Town As String 
     <DataMember()> Property County As String 
     <DataMember()> Property Postcode As String 
     <DataMember()> Property Latitude As Double
     <DataMember()> Property Longitude As Double
End Class

TextComplex 方法的请求 TextComplex

GET /Services/SubmitVehicle.svc/TestComplex?callback=jQuery15107153733184290069_1329385242312&Address%5B__type%5D=BasicAddress%3A%23WPC.FADS.Web&Address%5BBuilding%5D=Test&Address%5BCounty%5D=Test&Address%5BLatitude%5D=0&Address%5BLocality%5D=Test&Address%5BLongitude%5D=0&Address%5BPostcode%5D=test&Address%5BStreet%5D=Test&Address%5BTown%5D=Test&_=1329385253384 HTTP/1.1

方法的响应

jQuery15107153733184290069_1329385242312({"__type":"BasicAddress:#WPC.FADS.Web","Building":"Test","County":"Test","Latitude":0,"Locality":"Test","Longitude":0,"Postcode":"Nothing","Street":"Test","Town":"Test"});

TestSimple 有效,但 TestComplex 无效,我想用 TextComplex 实现的目标是否可能?

编辑

还按照 Zach 的建议尝试不使用 __type 参数:

var data = {
      Address: {
          "Building": "Test",
          "County": "Test",
          "Latitude": 0,
          "Locality": "Test",
          "Longitude": 0,
          "Postcode": "test",
          "Street": "Test",
          "Town": "Test"
      }
  }

操作合约:

   <OperationContract()>
   <WebGet(ResponseFormat:=WebMessageFormat.Json)>
   Function TestSimple(postcode As String) As BasicAddress

   <OperationContract()>
   <WebGet(ResponseFormat:=WebMessageFormat.Json)>
   Function TestComplex(Address As BasicAddress) As BasicAddress

I'm trying to pass information between domains using a combination of jQuery and WCF. This works really well for passing simple data types. This is demonstrated by the TestSimple Method below. However when I use the TestComplex method the WCF method receives nothing as the address parameter.

Hopefully an example will make my question clearer:

WCF Service:

 Function TestSimple(postcode As String) As BasicAddress Implements ISubmitVehicle.TestSimple
      Return Test(postcode)
 End Function

Function TestComplex(Address As BasicAddress) As BasicAddress Implements ISubmitVehicle.TestComplex
    If Address Is Nothing Then
        Return Test("Nothing")
    Else
        Return Test(Address.Postcode)
    End If
End Function

Function Test(postcode As String) As BasicAddress
    Return New BasicAddress With {
        .Building = "Test", .County = "Test", .Latitude = 0, .Locality = "Test",
        .Longitude = 0, .Street = "Test", .Town = "Test", .Postcode = postcode}
End Function

Client Script

function TestSimple() {
     var data = { postcode: "test" };
     Test(data, 'TestSimple');
}

function TestComplex() {
     // I also tried
     //var data = { Address: { Postcode: 'test' } }
     var data = { 
          Address: { "__type": "BasicAddress:#WPC.FADS.Web",
              "Building": "Test",
              "County": "Test",
              "Latitude": 0,
              "Locality": "Test",
              "Longitude": 0,
              "Postcode": "test",
              "Street": "Test",
              "Town": "Test"
          }
     }
     Test(data, 'TestComplex');
}

function Test(data, method) {
    $.getJSON(baseURL + 'Services/SubmitVehicle.svc/' + method + '?callback=?', data,
    function (NewBasicAddress) { alert(NewBasicAddress.Postcode); })
}

Data Contract:

<DataContract()>
Public Class BasicAddress
     <DataMember()> Property Building As String 
     <DataMember()> Property Street As String 
     <DataMember()> Property Locality As String 
     <DataMember()> Property Town As String 
     <DataMember()> Property County As String 
     <DataMember()> Property Postcode As String 
     <DataMember()> Property Latitude As Double
     <DataMember()> Property Longitude As Double
End Class

The Request for the TextComplex method

GET /Services/SubmitVehicle.svc/TestComplex?callback=jQuery15107153733184290069_1329385242312&Address%5B__type%5D=BasicAddress%3A%23WPC.FADS.Web&Address%5BBuilding%5D=Test&Address%5BCounty%5D=Test&Address%5BLatitude%5D=0&Address%5BLocality%5D=Test&Address%5BLongitude%5D=0&Address%5BPostcode%5D=test&Address%5BStreet%5D=Test&Address%5BTown%5D=Test&_=1329385253384 HTTP/1.1

The Response for the TextComplex method

jQuery15107153733184290069_1329385242312({"__type":"BasicAddress:#WPC.FADS.Web","Building":"Test","County":"Test","Latitude":0,"Locality":"Test","Longitude":0,"Postcode":"Nothing","Street":"Test","Town":"Test"});

TestSimple works but TestComplex does not, Is what I'm trying to achieve with TextComplex possible?

EDIT

also tried without the __type parameter as suggested by Zach:

var data = {
      Address: {
          "Building": "Test",
          "County": "Test",
          "Latitude": 0,
          "Locality": "Test",
          "Longitude": 0,
          "Postcode": "test",
          "Street": "Test",
          "Town": "Test"
      }
  }

Operation contracts:

   <OperationContract()>
   <WebGet(ResponseFormat:=WebMessageFormat.Json)>
   Function TestSimple(postcode As String) As BasicAddress

   <OperationContract()>
   <WebGet(ResponseFormat:=WebMessageFormat.Json)>
   Function TestComplex(Address As BasicAddress) As BasicAddress

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烛影斜 2025-01-13 08:04:08

经过一番研究,我认为答案是否定的。有关更多详细信息,请参阅WCF 服务的 REST/SOAP 端点

为了防止其他人遇到这个问题,我最终所做的是,对于我公开的每种方法,我创建了两个操作合约:

一个可以使用跨域客户端脚本中的“GetJSON”来使用,该脚本仅包含简单的参数。

一种包含复杂对象作为参数但无法从跨域客户端脚本使用的方法。

实现操作合约的方法都调用相同的代码。

After some research I think the answer is "no". See REST / SOAP endpoints for a WCF service for more details.

In case anyone else comes across this problem, what I did in the end is for each method I expose I created two operation contracts:

One which could be consumed using "GetJSON" from cross domain client script which included only simple parameters.

One which included complex objects as parameters but could not be consumed from cross domain client script.

The methods implementing the operation contracts both call the same code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文