从javascript调用C#webservices并使用它(json格式)

Ver*_*kis 6 javascript c# web-services

我已经创建了ac#webservice,我正在尝试调用它并从javascript脚本中使用它,这是什么方式或最好的方法,提前感谢.我将解释更多:这是Web服务:

 public class DocumentInfo : System.Web.Services.WebService
{

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public string GetDocumentInfo(string id)
    {
        Document document = new Document(Int32.Parse(id));    
        string output = JsonConvert.SerializeObject(document);
        return output;
    }
}
Run Code Online (Sandbox Code Playgroud)

我测试了它,它的工作原理,当我尝试建议的ajax解决方案时,我收到此错误500内部服务器错误.

Tal*_*lha 5

阅读一些教程

http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/ http://weblogs.asp.net/jalpeshpvadgama/archive/2010/08/29/calling-an-asp-净Web服务从- jquery.aspx

function TestService() 
{              
    try 
      {

       $.ajax({
         type: "POST",
         url: "http://webserviceURL.asmx/YourWebMethodName",
         data: "{'abc':'" + 123 + "'}", // if ur method take parameters
         contentType: "application/json; charset=utf-8",
         success: SuccessTestService,
         dataType: "json",
         failure: ajaxCallFailed
      });
    }
    catch (e)
    {
        alert('failed to call web service. Error: ' + e);
    }
}

function SuccessTestService(responce) {
    alert(eval(responce.d));
}


function ajaxCallFailed(error) {
        alert('error: ' + error);

    }
Run Code Online (Sandbox Code Playgroud)