JQuery调用WCF:.ajax工作,而.post不起作用

时间:2014-05-24 03:11:15

标签: jquery ajax wcf post

使用ajax调用WCF服务时,.ajax在.post没有错误时起作用:传入消息具有意外的消息格式“Raw”。该操作的预期消息格式是'Xml','Json'。

有人能说出这两个电话之间的区别吗?谢谢!

//WCF
    [OperationContract]
    public ColumnSelector[] returnColumnInformation(string templateid, string userid)
    {
    }

// ColumnSelector
[DataContract]
public class ColumnSelector
{
    [DataMember]
    public string ColumnName { get; set; }
    [DataMember]
    public string ColumnOrder { get; set; }
    [DataMember]
    public string ColumnDisplay { get; set; }

    public ColumnSelector()
    {
        ColumnName = "";
        ColumnOrder = "";
        ColumnDisplay = "";
    }

    public ColumnSelector(string _name, string _order, string _display)
    {
        ColumnName = _name;
        ColumnOrder = _order;
        ColumnDisplay = _display;
    }
}

// working:
        $.ajax({
            type: "POST",
            url: "AjaxService1.svc/returnColumnInformation",
            data: JSON.stringify({templateid: "1", userid: "300"}) ,
            dataType: "json",
            processData: true,
            contentType: "application/json; charset=utf-8",
            error: function (xhr, ajaxOptions, thrownError) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            }
        })
        .done(function (msg) {
         alert("POST Data Saved: " + msg);
        });


// not working: The incoming message has an unexpected message format 'Raw'. The expected message
// formats for the operation are 'Xml', 'Json'.
    $(document).ready(function () { 
        $("#AjaxPost").click(function () {
            $.post("AjaxService1.svc/postColumnInformation",
            {templateid: "1", userid: "300"},
            function (data, status) {
                alert("Data: " + data + "\nStatus: " + status);
            },
            "json"               
            )
             .done(function () {
                 alert("second success");
             })
              .fail(function (xhr, ajaxOptions, thrownError) {
                  var err = eval("(" + xhr.responseText + ")");
                  alert(err.Message);
              })
                .always(function () {
                    alert("finished");
                });
            return false;
        });

1 个答案:

答案 0 :(得分:0)

您在.post方法中请求的网址与.ajax方法中的网址不同,可能是由于该特定网址出现问题。 .post用法看起来是正确的,应该产生与.ajax用法相同的结果。 Reference.