Search code examples
jqueryjsonajaxasmx

jQuery ajax request from asmx web service


I'm attempting a request data from a HTTP POST asmx web service with jQuery's ajax. I've read numerous guides on how to do this correctly but with no success. From what I can determine, it is the request itself that is failing:

$.ajax({
    type: 'POST',
    url: "http://data.niassembly.gov.uk/organisations.asmx/GetPartiesListCurrent_JSON",
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({}),
    dataType: 'json',
    success: function(data){
        if (data.hasOwnProperty('d')){
            msg = data.d;
            } else {
            msg = data;
            } 
            alert(msg);
    }
    ,
    error:function(){
        alert('error');
        }
});

The JSON formatting is correct and when I save the content as a local .json file the function works. The service also has a GET option but from what I've read that won't work in this case.


Solution

  • Try This:

    $.ajax({
      type: 'POST',
      //url: "http://data.niassembly.gov.uk/organisations.asmx/GetPartiesListCurrent_JSON",
      url: "http://data.niassembly.gov.uk/organisations_json.ashx?m=GetPartiesListCurrent",
      contentType: 'application/json; charset=utf-8',
      data: JSON.stringify({}),
      dataType: 'jsonp',
      success: function(data) {
        alert(data.OrganisationsList.Organisation[1].OrganisationName);
    
      },
      error: function() {
        alert('error');
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>