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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12295492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:50:01  来源:igfitidea点击:

Call C# webservices from javascript and consume it (json format)

c#javascriptweb-services

提问by Vervatovskis

I have created a c# webservice and i am trying to call it and consume it from a javascript script, what is the way or the best way to do it, thanks in advance. I'll explain more: this is the web service:

我已经创建了 ac# webservice,我正在尝试调用它并从 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;
    }
}

I have tested it, it works, when i tried the suggested ajax solutions, i got this error 500 Internal Server Error.

我已经测试过它,它有效,当我尝试建议的 ajax 解决方案时,我收到此错误 500 内部服务器错误。

采纳答案by Talha

Read some of the tutorials

阅读一些教程

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

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);

    }

回答by alexandernst

You need to make an AJAX request and wait for the callback to receive the data.

您需要发出 AJAX 请求并等待回调接收数据。

A very simple example using jQuery:

使用 jQuery 的一个非常简单的示例:

$.ajax({
  url: "/my_service.cs"
}).done(function(data) { 
  console.log("Received: ", data);
});

回答by Gyan Chandra Srivastava

go through this demo project it will help you in multiple direction 

http://www.codeproject.com/Articles/21045/Different-methods-to-call-Web-Services-from-AJAX

http://www.codeproject.com/Articles/21045/Different-methods-to-call-Web-Services-from-AJAX

by using javascript ajax method you can do it

通过使用 javascript ajax 方法,你可以做到

  $.ajax({
                        type: "POST",
                        url: ,//webservice url
                        data: , // if ur method take parameters
                        contentType: "application/json; charset=utf-8",
                        success:{},
                        dataType: "json",
                        failure: {}
                    });

回答by Darren

Use jQuery AJAX:

使用jQuery AJAX

$.ajax({
  url: 'YourServiceURL',
  success: function(data) {
     alert('Web Service Called!');
  }
});

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/