【发布时间】:2014-02-17 11:41:34
【问题描述】:
好的,我已经为此工作了几天,它开始让我烦恼。
我有一个页面,用户可以在其中更改配色方案。并为页面选择一个徽标。
使用jquery循环所有可以更改的项目($(“.brand”).each)
并构建数据,最后将其作为 json 对象发送到 wcf 服务,见下文
$(".brand").each(function () {
//use the title attribute to list the css properties
// you want for that element
//use the id with a prefix to represent the actual element
// you want to brand,
//matching up with the item in the site's css
//prefix 'c-' = css class so replace with '.'
//prefix 'id-' = element id so replace with '#'
//prefix 'e-' = element so just remove the prefix
var id = $(this).attr("id").replace("c-", ".").replace("id-", "#").replace("e-", "");
var title = $(this).attr("title");
var values = title.split(',');
var property = "";
var value = "";
for (var i = 0; i < values.length; i++) {
selector = values[i]
value = $(this).css(values[i]);
}
var item = {};
item["id"] = "";
item["selector"] = id;
item["css_property"] = property;
item["property_value"] = value;
json.push(item);
});
if ($(".imgbase").val().length > 0) {
var logoUrl = $(".imgbase").val();
logoUrl = logoUrl.replace(new RegExp("^data:image/[a-z]*;base64,", ""));
var item = {};
item["id"] = 1;
item["selector"] = "";
item["css_property"] = "";
item["property_value"] = logoUrl;
json.push(item);
}
$.ajax({
type: "POST",
contentType: "application/json",
url: "http://localhost:64177/BrandingService.svc/DoBranding",
data: JSON.stringify({ CSS: json }),
dataType: "json",
success: function (msg) {
if (msg.hasOwnProperty("d"))
alert(msg.d);
},
error: function (result) {
alert("Failed to call service: (" + result.status + ") \n" + result.statusText);
}
});
现在这似乎创建了一个数组对象,所以我的问题是,我的服务到底应该期待什么,我该如何阅读它?假设我发送正确?如果我将它作为对象接收,则没有错误,但服务不知道它是什么并且无法反序列化它。我无法将其作为 List(Of BrandingCSS) 接收,这会导致 500 错误,我有一个类(见底部),我试图将其用作 List(Of BrandingCSS),那么我如何获得“ CSS 对象”到那个?我已经尝试过 JavaScriptSerializer 和 Json.net,我愿意接受任何一个来获得结果,所以如果有人可以提供帮助,请在我发疯之前做。
<OperationContract()>
Public Function DoBranding(ByVal CSS As Object) As String
Try
Return "FOO"
Catch ex As Exception
Return "BAR: " & ex.Message
End Try
End Function
我正在使用的课程
<DataContract([Namespace]:="")> _
Public Class BrandingCSS
<DataMember>
Public Property ServiceID() As Integer
Get
Return m_ServiceID
End Get
Set(value As Integer)
m_ServiceID = value
End Set
End Property
Private m_ServiceID As Integer
<DataMember>
Public Property selector() As String
Get
Return m_selector
End Get
Set(value As String)
m_selector = value
End Set
End Property
Private m_selector As String
<DataMember>
Public Property css_property() As String
Get
Return m_property
End Get
Set(value As String)
m_property = value
End Set
End Property
Private m_property As String
<DataMember>
Public Property property_value() As String
Get
Return m_value
End Get
Set(value As String)
m_value = value
End Set
End Property
Private m_value As String
<DataMember>
Public ReadOnly Property logo() As Byte()
Get
Return img
End Get
End Property
Private img As Byte() = Nothing
Public Sub New()
Try
img = Convert.FromBase64String(property_value)
Catch ex As Exception
img = Nothing
End Try
End Sub
End Class
如果您想查看 web.config 中的服务部分,就是这个
<system.serviceModel>
<services>
<service name="BrandingService">
<endpoint address="" behaviorConfiguration="BrandingServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="BrandingService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="BrandingServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="5000000" />
</webServices>
</scripting>
</system.web.extensions>
发送的 json 样本在这里
"[
{\"id\":\"1\",\"selector\":\".mp-level\",\"css_property\":\"background\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\".mp-level\",\"css_property\":\"color\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"#header\",\"css_property\":\"background\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"#header\",\"css_property\":\"color\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"#header\",\"css_property\":\"border-bottom-color\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"headerinput\",\"css_property\":\"background\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"headerbutton\",\"css_property\":\"background\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"footer\",\"css_property\":\"background\",\"property_value\":\"\"},
{\"id\":\"1\",\"selector\":\"footer\",\"css_property\":\"color\",\"property_value\":\"\"}
]"
【问题讨论】:
-
是RESTFull服务吗?
-
它是网站本身内的 WCF 服务,我尝试将 RESTFull 服务作为其外部的单独项目,结果相同。