好的,我已经做了几天了,现在开始惹恼我了。
我有一个页面,用户可以在其中更改颜色方案。并为页面选择一个徽标。
使用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\":\"\"}
]"
答案 0 :(得分:0)
首先,添加
contentType: "application/json;charset=utf-8"
在你的POST请求标题中。
我的服务合同中没有看到RequestFormat = WebMessageFormat.Json
。
C#代码
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "CSSBranding",
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
string DoBranding(BrandingCSS[] css);
请注意,您的JSON和 BrandingCSS 类应具有相同的参数。例如,对于JSON中的“selector”,您必须在 BrandingCSS 类中具有“ selector ”属性。
[DataContract(Namespace = "")]
public class BrandingCSS
{
[DataMember]
public string selector {get; set;}
[DataMember]
public string id {get; set;}
//Remaining properties here.
}
我希望您了解 C#代码。
修改强> 如果您想查看您的服务应接受的JSON,请覆盖 BrandingCSS 类的 ToString()方法。
public override string ToString()
{
JavaScriptSerializer js = new JavaScriptSerializer(); // Available in System.Web.Script.Serialization;
return js.Serialize(this);
}
现在创建一个类 BrandingCSS 的对象。
BrandingCSS bcss = new BrandingCSS();
string acceptedJson = bcss.ToString();
Console.WriteLine(acceptedJson);
或者你可以制作一个临时的OperationContract并调用这些代码行。点击那里的断点,看看JSON。之后,创建一个刚刚在调试器中看到的JSON并发送请求。见500 Internal Server Error.。而且您还没有显示 Web.Config 文件。