用钛合金向WCF发送post数据

用钛合金向WCF发送post数据,wcf,titanium,Wcf,Titanium,以下是钛的代码: var request = Titanium.Network.createHTTPClient(); request.open("POST", bh.serverAddress + "MyCareer.svc/PostMessage/"+ bh.userID + "/" + bh.logic.profile.userID); request.setRequestHeader("enctype", "multipart/form-data");

以下是钛的代码:

var request = Titanium.Network.createHTTPClient();

        request.open("POST", bh.serverAddress + "MyCareer.svc/PostMessage/"+ bh.userID + "/" + bh.logic.profile.userID);
        request.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');
        };
以下是WCF的代码:

接口:

[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);
类别:

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;
        }
查询:如果我将此功能转换为“GET”,那么它的工作效果会非常好。但是,有了“POST”,我就得到了错误,我无法找出错误。我也为WCF启用了traceListener,但没有错误


请帮忙。我被困在这一点上。我正在试用iPhone模拟器。

您需要按正确的顺序设置代码

request.open(“POST”,bh.serverAddress+“MyCareer.svc/PostMessage/”+bh.userID+“/”+bh.logic.profile.userID)


根据文档,该片段需要在侦听器之后(onload、onerror等…)和
send()
之前。

您需要按照正确的顺序设置代码

request.open(“POST”,bh.serverAddress+“MyCareer.svc/PostMessage/”+bh.userID+“/”+bh.logic.profile.userID)


根据文档,该片段需要在侦听器之后(onload、onerror等…)和
send()
之前。

最后,我在代码中发现了问题。此解决方案适用于所有客户端技术

让我们先看看工作代码:

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); 
修正:

  • 我发送的json数据格式不正确。我创建了一个json字符串,因为它应该在本地javascript中使用。但是,我的json必须被WCF使用。因此,当它到达那里时,它应该仍然是json。请注意单引号和双引号的使用。他们应该像现在这样。我认为交换它们也不应该起作用,因为Asp.net中的单引号表示字符而不是字符串
  • 注意我发送的标题信息的使用。我认为它应该有一些变化,但至少需要application/json

  • 希望有帮助。关键是,您认为对javascript正确的东西对json不正确。

    最后,我在代码中发现了问题。此解决方案适用于所有客户端技术

    让我们先看看工作代码:

    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); 
    
    修正:

  • 我发送的json数据格式不正确。我创建了一个json字符串,因为它应该在本地javascript中使用。但是,我的json必须被WCF使用。因此,当它到达那里时,它应该仍然是json。请注意单引号和双引号的使用。他们应该像现在这样。我认为交换它们也不应该起作用,因为Asp.net中的单引号表示字符而不是字符串
  • 注意我发送的标题信息的使用。我认为它应该有一些变化,但至少需要application/json

  • 希望有帮助。关键是,您认为对javascript正确的东西对json不正确。

    我验证并发现它在任何一种情况下都不起作用。但是,有一点需要注意:Tianium实际上向WCF发送了一个成功的请求,因为调用了我的onload函数。我认为问题在于Tianium和WCF之间关于邮政服务的通信签名。让我检查Asp.Net页面,而不是WCF。谢谢你,因为我在你的答案上面的评论,我终于找到了错误。非常感谢。我验证了一下,发现两种方法都不起作用。但是,有一点需要注意:Tianium实际上向WCF发送了一个成功的请求,因为调用了我的onload函数。我认为问题在于Tianium和WCF之间关于邮政服务的通信签名。让我检查Asp.Net页面,而不是WCF。谢谢你,因为我在你的答案上面的评论,我终于找到了错误。多谢。