简体   繁体   English

测试从某些JavaScript返回JSON的ASP.NET Webservice的最简单方法是什么?

[英]Simplest way to test ASP.NET Webservice that returns JSON from some JavaScript?

We have a .asmx service that returns JSON data. 我们有一个返回JSON数据的.asmx服务。 Can someone point me to a simple example that calls the service from a page w JavaScript ? 有人能指出一个简单的例子,从一个页面调用JavaScript服务吗? Tks TKS

You can use jQuery's get function. 你可以使用jQuery的get函数。

var dataID = $('#WhatINeedForAParameter').val();
$.get('WebApiAddress/MethodName', { id: dataID }, 
   function(data) {
     alert(data);
     // Since your method returns a JSON object you can access
     // the properties of that object
     alert(data.id);
     alert(data.name);
});

Or if you want to use the long hand jQuery ajax you can do the following: 或者如果你想使用长手jQuery ajax,你可以执行以下操作:

var dataID = $("#WhatINeedForAParameter").val();
var request = $.ajax({
  url: "WebApiAddress/MethodName",
  type: "POST",
  data: { id : dataID },
  dataType: "jsonp"
});

request.done(function(msg) {
  alert('You got this ' + msg);
});

request.fail(function(jqXHR, textStatus) {
  alert( "Your request failed: " + textStatus );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM