0

I have a link that looks like this :

 //GetAll

        [OperationContract]

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

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

And I'm trying to consume this with jQuery Ajax, and I've made something like this:

$("#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);
        }
    })
});

But i think that a POST request that returns some data cannot be consumed here and I cannot fins any solutions for me.

Can anybody help me?

UPDATE:

button code (outside any form):

R.Quintal
  • 43
  • 1
  • 3
  • 13
  • 1
    Is something not working here? POST returning some data is totally fine, so it should work – Andrey Feb 23 '18 at 14:09
  • @Andrey yes, nothing its working here. I've been struggling into this by one day, got no error in the console, nothing. When I hit the button the page just reloads for no reason and get no response. – R.Quintal Feb 23 '18 at 14:44
  • 1
    I believe your button is in form and form got submitted. These answers could help you https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted – Andrey Feb 23 '18 at 14:47
  • @Andrey thank you in advance for helping me bud. Very very thank you, but my button is not inside any form, and I'm going crazy bout this. I've updated the question with a print screen – R.Quintal Feb 23 '18 at 16:08

1 Answers1

-1

try this

$.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);
        }
    })
});
China Syndrome
  • 953
  • 12
  • 24