sending post data to WCF in Titanium
Below is the code for titanium:
var request = Titanium.Network.createHTTPClient();
request.open("POST", bh.serverAddress + "MyCareer.svc/PostMessage/"+ bh.userID + "/" + bh.logic.profile.userID);
reques开发者_开发知识库t.setRequestHeader("enctype", "multipart/form-data");
request.setRequestHeader("Content-Type", "text/json");
request.send(data_to_send);
request.onload = function() {
Ti.API.info(this.responseText);
bh.ui.profile.createWindow();
};
request.onerror = function(){
alert('Error while posting message');
};
Below is the code for WCF:
Interface:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/PostMessage/{userid}/{touserid}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
int PostMessage(string userid, string touserid, string message);
Class:
public int PostMessage(string userID, string toUserID, string message)
{
MDBDataContext oMDB = new MDBDataContext();
int returnValue = oMDB.PostMessage(Convert.ToInt32(userID), message, Convert.ToInt32(toUserID));
oMDB.Dispose();
return returnValue;
}
Query: If i convert this functionality to "GET" than it works very fine. But, with "POST" i get error and i am unable to figure out the error. I have enable traceListener also for WCF, but no error there.
Please help. I am stuck at this point. I am trying with iPhone simulator.
Finally, i found the problem in my code. And this solution apply to every client side technology.
Let's see the working code first:
var data_to_send = '{"userid": "' + bh.userID + '", "touserid": "' + bh.logic.profile.userID + '","message": "' + bh.ui.postMessage.txtPost.value + '"}';
var request = Titanium.Network.createHTTPClient();
request.onload = function() {
//Some code here
};
request.onerror = function(){
//Some code here
};
request.open("POST", bh.serverAddress + "MyCareer.svc/PostMessage");
request.setRequestHeader("enctype", "multipart/form-data");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.send(data_to_send);
Fixes:
- The json data that i was sending was not in correct format. I created a json string as it should had been consumed in the local javascript. But, my json had to be consumed by WCF. So, it should remain json by the time it reaches there. Notice the use of single and double quotes. They should be like they are. I think interchanging them should also not work because single quotes in Asp.net means a character and not string.
- Notice the use of header information that i am sending. I think some variation in it shall work, but atleast application/json thing is required.
Hope that helps. whole point is that what you deem is correct for javascript is not correct for json.
You need to set you're titanium code in the proper order.
request.open("POST", bh.serverAddress + "MyCareer.svc/PostMessage/"+ bh.userID + "/" + bh.logic.profile.userID);
That snippet needs to be after the listeners(onload, onerror, etc...) and before the send()
according to the documentation.
精彩评论