locked
calling a webservice from the client with ajax RRS feed

  • Question

  • User65893105 posted

    I have a webservice, defined as below

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class AudioWebService : System.Web.Services.WebService
    {

    [WebMethod(Description = "Gets a list of audio files for a specific artist")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<AudioItem> GetAudio(string artistId)
    {
    BusinessLayer.AudioBLManager audioManager = new AudioBLManager();
    List<AudioItem> res = audioManager.GetAudio(artistId);
    return res;
    }
    }

    if I put this into a browser, its works fine and I can see and invoke the web method

    http://localhost/Website/AudioWebService.asmx?op=GetAudio

    when I execute this code from a web page, I get an internal server error 500

     

    var test = function CallService(cb) {


    var webMethod = "http://localhost/Website/AudioWebService.asmx/GetAudio";

    $.ajax(
    {
    Type: "POST",
    contentType: "application/json; charset=utf-8",
    url: webMethod,
    data: { artistId: $("#" + '<%= HiddenArtistID.ClientID %>').val() },
    success: cb,
    error: function (e) { alert(e.statusText); }
    });

    }

    test(callback);

    im really stuck can anyone help ?

    Friday, October 11, 2013 6:02 AM

Answers

  • User65893105 posted

    worked it out for myself in the end, just needed to add this attribute to the webmethod

    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 11, 2013 9:32 AM

All replies

  • User-1360095595 posted

    var webMethod = '<%= ResolveUrl("~/AudioWebService.asmx/GetAudio") %>';

    Friday, October 11, 2013 6:19 AM
  • User65893105 posted

    thanks, I tried that but I got exactly the same error

    Friday, October 11, 2013 6:28 AM
  • User65893105 posted

    putting this in a browser

    http://localhost/Website/AudioWebService.asmx/GetAudio?artistId=445

    works fine and gives me results in xml

    Friday, October 11, 2013 6:33 AM
  • User65893105 posted

    anyone ??

    Ive checked with fiddler and Im getting this error message

    webservice ajax asp.net An attempt was made to call the method GetAudio using a GET request, which is not allowed

    but Im calling it using this syntax

    $.ajax(
    {
    Type: "POST",
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    url: webMethod,
    data: { artistId: $("#" + '<%= HiddenArtistID.ClientID %>').val() },
    success: cb,
    error: function (e) { alert(e.statusText); }
    });

    Friday, October 11, 2013 9:09 AM
  • User65893105 posted

    worked it out for myself in the end, just needed to add this attribute to the webmethod

    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 11, 2013 9:32 AM