It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




已关闭8年。




我已经创建了一个C#网络服务,我正试图调用它并从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;
    }
}

我已经对其进行了测试,当我尝试了建议的Ajax解决方案时,它出现了此错误500 Internal Server Error。

最佳答案

阅读一些教程

http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
http://weblogs.asp.net/jalpeshpvadgama/archive/2010/08/29/calling-an-asp-net-web-service-from-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);

    }

关于c# - 从javascript调用C#webservices并使用它(json格式),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12295492/

10-09 13:24