jQuery Ajax POST请求获取数据

时间:2018-02-23 14:07:10

标签: javascript jquery ajax web-services post

我的链接看起来像这样:

 //GetAll

        [OperationContract]

        [WebInvoke(Method = "POST", UriTemplate = "GetAll/{Token}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare )]

        List<DataModal.Main.Company > GetAll(string Token);

我正在尝试使用jQuery Ajax来使用它,我做了类似的事情:

$("#btnCom").click(function(){
    $.ajax({
        type: "POST", //REQUEST TYPE
        dataType: "json", //RESPONSE TYPE
        url: "http://ws/method/token", // URL OF THE WS
        success: function(data) {
            $.each(data, function(i) {
                if (data.length != i) {
                    $('#list').append("<option>" + data[i].Name + "</option>"); //fILL THE DDL. FOR EACH ITEM RETURNED ADD DATA[NAME] TO ONE LINE OF THE DDL.
                }
            });
        },
        error: function(err) {
            console.log("AJAX error in request: " + JSON.stringify(err, null, 2));
        }
    }).always(function(jqXHR, textStatus) {
        if (textStatus != "success") {
            alert("Error: " + jqXHR.statusText);
        }
    })
});

但我认为返回某些数据的POST请求不能在这里消费,我不能为我提供任何解决方案。

有人能帮助我吗?

更新:

按钮代码(任何形式外):

1 个答案:

答案 0 :(得分:-1)

试试这个

$.ajax({
        type: "POST", //REQUEST TYPE
        dataType: "json", //RESPONSE TYPE
        url: "http://ws/method/token", 
        data: (
        {
            token: put token here
        }),
        success: function(data) {
            $.each(data, function(i) {
                if (data.length != i) {
                    $('#list').append("<option>" + data[i].Name + "</option>"); //fILL THE DDL. FOR EACH ITEM RETURNED ADD DATA[NAME] TO ONE LINE OF THE DDL.
                }
            });
        },
        error: function(err) {
            console.log("AJAX error in request: " + JSON.stringify(err, null, 2));
        }
    }).always(function(jqXHR, textStatus) {
        if (textStatus != "success") {
            alert("Error: " + jqXHR.statusText);
        }
    })
});