[英]Wcf jsonp ajax jquery callback - how to customize the response, the name of the callback function, and the wrapper?
我需要構建一個 wcf 服務,它使用自定義響應,采用 jsonp 格式,我應該得到如下示例的響應:
調用此示例方法:
http://localhost:49350/Service1.svc/TestConnectionWCF_new?callback=jQuery22406768180963924506_1639677943363&_=1639677943368
我必須得到這個答案:
jQuery22406768180963924506_1639677943363( {"d":"<ACCESS><MSG><![CDATA[YES]]><\/MSG><\/ACCESS>"} );
我得到了這個:
{"TestConnectionWCF_NEWResult":"<ACCESS><MSG><![CDATA[YES]]><\/MSG><\/ACCESS>"}
我已經嘗試了很多方法,但我還沒有超越這個,如果有人能以任何方式幫助我,即使對 go 提出一些建議。
我做了一個簡單的服務測試 wcf,這是我正在使用的代碼
界面:
<ServiceContract>
Public Interface IService1
<OperationContract>
<WebGet(ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped)>
Function TestConnectionWCF_NEW() As Object
End Interface
服務:
Imports Test_Jsonp_Vb
Imports System.ServiceModel.Activation
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)>
Public Class Service1
Implements IService1
Dim oReturnObject As Object
Public Function TestConnectionWCF_NEW() As Object Implements IService1.TestConnectionWCF_NEW
Try
oReturnObject = "<ACCESS><MSG><![CDATA[YES]]></MSG></ACCESS>"
Return oReturnObject
Catch ex As Exception
oReturnObject = ex.Message
Return oReturnObject
End Try
End Function
End Class
服務標記:
<%@ ServiceHost Language="VB" Debug="true" Service="Test_Jsonp_Vb.Service1" CodeBehind="Service1.svc.vb" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
網絡配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
我設法以某種方式解決了它,我不知道這是否是最好的方法,但是我解決了我的問題,如下所示:
服務:我刪除了接口文件,添加了 MyType class,它具有可設置的 object “d”屬性,然后我將其作為來自 wcf 服務的回調返回。
Imports System.ServiceModel.Activation
<ServiceContract()>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class Service
Dim oReturnObject As Object
Public Class MyType
Public Property d As Object
End Class
<WebGet(ResponseFormat:=WebMessageFormat.Json)>
Public Function TestConnectionWCF_new() As MyType
Try
oReturnObject = "<ACCESS><MSG><![CDATA[YES]]></MSG></ACCESS>"
Catch ex As Exception
oReturnObject = ex.Message
End Try
Return New MyType With {.d = oReturnObject}
End Function
End Class
服務標識:
<%@ ServiceHost Language="VB" Debug="true" Service="Wcf_Jsonp2.Service" CodeBehind="Service.svc.vb" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
Web 配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<!-- ... -->
<services>
<service name="Jsonp">
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
<!-- ... -->
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<!-- ... -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
<!-- ... -->
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true" name=""/>
</webScriptEndpoint>
</standardEndpoints>
<!-- ... -->
</system.serviceModel>
<!-- ... -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
現在調用服務我得到這樣的答案,這正是我需要的......
jQuery22406768180963924506_1639677943363({"__type":"Service.MyType:#Wcf_Jsonp2","d":"<ACCESS><MSG><![CDATA[YES]]><\/MSG><\/ACCESS>"});
感謝“James Z”,因為該鏈接對我幫助很大。
我希望它對你有用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.