谢谢您的回答。
我对我的API的一部分使用SOAP,但我也使用webhttp端点 进行REST风格的xhr调用和JSON响应。我已经能够在节目中做到这一点 别处:
var url = "trade.svc/GetCategoryChildren/" + parentID + "/json";
var httpStatus;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
我试图做同样的事情对我的上传服务,但是,正如你所指出 出来,收集List<Upload> request = new List<Upload>();
是C#,JavaScript不 和我我不太确定如何以REST方式传递参数。这可能吗? 想法感激?
<service behaviorConfiguration="Default" name="WebClient.UploadService">
<endpoint address="json" behaviorConfiguration="WebHttpEPBehavior"
binding="webHttpBinding" name="MyJSONUploadEP" contract="WebClient.IJSONUploadService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:2534/Web/upload.svc" />
</baseAddresses>
</host>
</service>
响应萨哈 -
请求应类似于我在以前的文章中概述的XMLHTTP请求对象。您希望将您的端点作为xmlhttp.open(“GET”,url,true)中的URL参数传递。你的参数应该遵循相同的约定:“service.svc/function/param1/param2/param3”。
在服务器上,您想要将服务的绑定设计为webHttpBinding - 在我的发布配置文件中查看MyJSONUploadEP。然后,在您的合同中(在我的示例中为WebClient.IJSONUploadService),您希望为请求的方法指定带有JSON重定义格式的WebGet属性。在我的合同,我有:
[WebGet(UriTemplate = "GetCategoryChildrenJSON/{parentID}/{format}", ResponseFormat = WebMessageFormat.Json)]
Tuple<string, int> GetCategoryChildren(string parentID, string format);
顺便说一句,我认为应该有不同的响应格式不同的合同(IJSONUploadService.cs,IPOXUploadService.cs和ISOAPUploadService.cs根据需要然后将其继承)。因此,举例来说,您的WCF类看起来是这样的:
public class UploadService : IJSONUploadService
如果你想在你的JSON响应返回一个整数数字,你应该设计具有get/set整数类型的支撑类 - 东西像:
public class Items
{
public int PropA { get; set; }
public int PropB { get; set; }
public int PropC { get; set; }
public int PropD { get; set; }
public int PropE { get; set; }
etc...
}
然后,从数据库中检索数据后,你将需要填充 您(在这种情况下,类型项目)名单:
List<Items> categories = new List<Items>();
接下来,您将需要序列化您的发送 到客户端的类型项目列表。我正在使用名为JSONHelper的类来执行此操作,但在Internet上有大量示例。您可以先查找
“System.Runtime.Serialization.Json”命名空间。以下是我对 序列化对象的样子:
string dtSerialized = JSONHelper.Serialize<List<Items>>(categories);
Tuple<int, int> retParam = new Tuple<int, int>(dtSerialized, errCode);
return retParam;
最后,在客户端上,你将需要处理使用某种JavaScript的响应处理的JSON响应。这里有很多这种在线的例子 - 你可能想 尝试从http://json.org/js.html开始。
HTH, 彼得
马克 - 我的反应是在上述(或低于) “接听”。谢谢你的想法。 – 2009-10-20 14:06:02