Asp.net的WebMethod - 返回字符串[]和使用JavaScript解析 [英] Asp.net WebMethod - return string[] and parse it using JavaScript

查看:62
本文介绍了Asp.net的WebMethod - 返回字符串[]和使用JavaScript解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从的MyMethod为codebehind返回字符串数组。但不要用我分析它在aspx页面的JavaScript

  [WebMethod的]
公共静态字符串[]的MyMethod(){
   返回新的[] {fdsf,gfdgdfgf};
}

..........
功能myFunction的(){
            $阿贾克斯({......
                    成功:函数(MSG){
                                //我怎么解析味精?
                                }
            });
        };
 

解决方案

首先,确保你已经标记了类 [ScriptService] ,以允许它被称为通过AJAX。是这样的:

  [ScriptService] //<  - 重要
公共类的WebService:System.Web.Services.WebService
{
   [ScriptMethod] //<  -  WebMethod的是在这里很好过
   公共字符串[]的MyMethod()
   {
      返回新的[] {fdsf,gfdgdfgf};
   }
}
 

您可以再读取结果与 jQuery的直接,因为没有必要的解析<​​/ em>的东西:

  $(文件)。就绪(函数(){
  $阿贾克斯({
    键入:POST,
    网址:WebService.asmx /的MyMethod
    数据: {},
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON,
    成功:函数(MSG){
      // msg.d将是你的阵列,2串
    }
  });
});
 

另一种方法是只包含一个参考:

 &LT;脚本SRC =WebService.asmx / JS类型=文/ JavaScript的&GT;&LT; / SCRIPT&GT;
 

这将生成代理类,允许你直接调用Web方法。例如:

  WebService.MyMethod(的onComplete,onError的);
 

的onComplete 函数将接受一个参数与Web服务调用的结果,你的情况一个Javascript数组2串。在我看来,这比使用jQuery和担心URL和HTTP有效载荷更容易的解决方案。

I need to return an array of string from MyMethod at codebehind. But do I parse it on aspx page using javascript?

[WebMethod]
public static string[] MyMethod(){
   return new[] {"fdsf", "gfdgdfgf"};
}

..........
function myFunction() {
            $.ajax({ ......
                    success: function (msg) {
                                //how do I parse msg?
                                }
            });
        };

解决方案

First, make sure you've tagged your class with [ScriptService] to allow it to be called through AJAX. Something like:

[ScriptService] //<-- Important
public class WebService : System.Web.Services.WebService
{
   [ScriptMethod] //<-- WebMethod is fine here too
   public string[] MyMethod()
   {
      return new[] {"fdsf", "gfdgdfgf"};
   }
}

You can then read the result with jQuery directly, as there's no need to parse anything:

$(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "WebService.asmx/MyMethod",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      // msg.d will be your array with 2 strings
    }
  });
});

Another approach is to just include a reference to:

<script src="WebService.asmx/js" type="text/javascript"></script>

This will generate proxy classes to allow you to call web methods directly. For example:

WebService.MyMethod(onComplete, onError);

The onComplete function will receive a single parameter with the results of the web service call, in your case a Javascript array with 2 strings. In my opinion, this is an easier solution than using jQuery and worrying about the URL and HTTP payload.

这篇关于Asp.net的WebMethod - 返回字符串[]和使用JavaScript解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆