[英]How to pass parameters to a WCF REST method using WinJS.xhr?
I am trying to pass parameters to my WCF which uses REST to pass data. 我试图将参数传递给我的WCF,它使用REST传递数据。
The definition of my method is : 我方法的定义是:
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void newUserAndImageEntry(byte[] pArrayImage, string pContentType, string pUserName, string pFileName);
What I am trying is sort of : 我正在尝试的是:
WinJS.xhr({ url: "http://localhost:9814/Student.svc/newUserAndImageEntry" })
.then(function (r ) {
DO WHAT?;
});
But dont know what to do in the function or if I have to pass my parameters in advance.. 但是不知道在函数中做了什么,或者我是否必须事先传递我的参数..
Your operation as is won't work - since you have more than one parameter, you need to define the BodyStyle
property as Wrapped
(or WrappedRequest
- in your scenario, since the operation doesn't have a return value, it doesn't matter): 您的操作不起作用 - 因为您有多个参数,您需要将
BodyStyle
属性定义为Wrapped
(或WrappedRequest
- 在您的方案中,因为操作没有返回值,所以无关紧要):
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void newUserAndImageEntry(byte[] pArrayImage, string pContentType,
string pUserName, string pFileName);
Another problem is that byte arrays probably isn't a good type to receive data from JavaScript - it will be received as an array of numbers which is not very efficient. 另一个问题是字节数组可能不是从JavaScript接收数据的好类型 - 它将作为数字数组接收,效率不高。 Doing some preprocessing on the client - for example, to encode the bytes as base64, will give you a smaller payload
在客户端上进行一些预处理 - 例如,将字节编码为base64,将为您提供更小的有效负载
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void newUserAndImageEntry(string pArrayImageAsBase64, string pContentType,
string pUserName, string pFileName);
Now for the client side: you need to pass your parameters in the data
field in the object you pass as a parameter. 现在,对于客户端:您需要在作为参数传递的对象的
data
字段中传递参数。 Something like the code below. 类似下面的代码。 Check the WinJS.xhr documentation for more details on the call.
有关呼叫的更多详细信息,请查看WinJS.xhr文档 。
var arrayImage = getArrayImage();
var arrayImageBase64 = convertToBase64(arrayImage);
var contentType = 'image/jpeg';
var userName = 'johndoe';
var fileName = 'balls.jpg';
var data = {
pArrayImageAsBase64: arrayImageBase64,
pContentType: contentType,
pUserName: userName,
pFileName: fileName
};
var xhrOptions = {
url: "http://localhost:9814/Student.svc/newUserAndImageEntry",
headers: { "Content-Type": "application/json" },
data: JSON.stringify(data)
};
WinJS.xhr(xhrOptions).done(
function (req) {
// Call completed, find more info on the parameter
}, function (req) {
// An error occurred, find more info on the parameter
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.