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

标签 c# javascript web-services

<分区>

我已经创建了一个 c# web 服务,我正在尝试调用它并从 javascript 脚本中使用它,这是什么方法或最好的方法,在此先感谢。 我会解释更多: 这是网络服务:

 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/

相关文章:

javascript - 如何以编程方式删除 WebSQL 中的数据库?

javascript - 拒绝 WebRTC 提议的正确方法是什么?

javascript - 如何在三个js中制作3d文字

java - 使用 RestTemplate 反序列化嵌套对象

php 获取函数后的结果

C# Roslyn API,读取 .cs 文件,更新类,写回 .cs 文件

c# - 如何从目录导入视频?

javascript - 在浏览器中渲染文件

c# - 在 C# 中使用工厂设计模式的示例

c# - 如何为应用程序设置样式,以便所有控件均显示在应用程序的配色方案中