jQuery ajax request from asmx web service

给你一囗甜甜゛ 提交于 2020-01-05 11:11:10

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/35244965/jquery-ajax-request-from-asmx-web-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!