【问题标题】:Calling WebMethod with parameters from jQuery ajax fails使用来自 jQuery ajax 的参数调用 WebMethod 失败
【发布时间】:2014-05-29 09:50:26
【问题描述】:

我对以下问题感到困惑。

我有一个“WebForm1.aspx”和一个“WebService1.asmx”。 当我在没有参数('HelloWorld')的 WebService 中调用 WebMethod 时,它工作正常。当我调用带有参数的方法('SayHello')时,它失败了。

它甚至没有命中方法(我在方法中设置的断点没有到达)。 xmlHttpRequest 错误是“内部服务器错误”

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string SayHello(string firstName, string lastName)
    {
        return "Hello " + firstName + " " + lastName;
    }
}



我的 WebForms1.aspx:

    <div><br />No Parameters </div>
<div id="NoParameters"></div>
<div><br />With Parameters</div>
<div id="WithParameters"></div>


<script type="text/javascript">
    $(document).ready(function () {
        // SayHello returns a string we want to display.  Examples A, B and C show how you get the data in native
        // format (xml wrapped) as well as in JSON format.  Also how to send the parameters in form-encoded format,
        // JSON format and also JSON objects.  To get JSON back you need to send the params in JSON format.

        // Test - call a function that returns a string.
        // No Parameters
        $.ajax({
            type: "POST",
            url: "WebService1.asmx/HelloWorld",
            data: "{}",
            dataType: "text",
            success: function (data) {
                $("#NoParameters").html(data); // show the string that was returned, this will be the data inside the xml wrapper
            }
        });

        // Example A - call a function that returns a string.
        // Params are sent as form-encoded, data that comes back is text
        $.ajax({
            type: "POST",
            url: "WebService1.asmx/SayHello",

            //data: "firstName=Aidy&lastName=F", // the data in form-encoded format, ie as it would appear on a querystring
            //contentType: "application/x-www-form-urlencoded; charset=UTF-8", // if you are using form encoding, this is default so you don't need to supply it

            data: "{firstName:'Aidy', lastName:'F'}", // the data in JSON format.  Note it is *not* a JSON object, is is a literal string in JSON format
            contentType: "application/json; charset=utf-8", // we are sending in JSON format so we need to specify this

            dataType: "text", // the data type we want back, so text.  The data will come wrapped in xml
            success: function (data) {
                $("#WithParameters").html(data); // show the string that was returned, this will be the data inside the xml wrapper
            }
            , error: function(xmlHttpRequest, status, err) {alert(err);}
        });
    });

我使用的代码来自互联网上的示例。我尝试以表单编码(已注释掉)和 JSON 的形式传递参数。似乎没有任何效果。

怎么办?

  • 会不会是jQuery 1.8.2的版本?
  • 我应该使用“提琴手”吗? (还没用过,学习曲线)
  • IE11 中的 F12 开发人员工具会有所帮助吗?
  • IIS Express 是否在我的开发 PC 上本地运行?

任何帮助将不胜感激。

我的浏览器有什么问题吗?谷歌搜索页面上显示了很多上面带有波浪号的“A”,我在开发人员机器上从 IE 提交这个问题时遇到了问题。在我的普通电脑上提交很好。某种编码问题?

【问题讨论】:

  • 我在另一台 PC 上尝试了整个项目。相同的操作系统,相同的 VS2013 相同的 IE11 并且它可以工作!。

标签: jquery ajax webforms webmethod


【解决方案1】:

在 ajax 调用中更改你的 "data" 参数,例如 数据:JSON.stringify({ Msg: 'Hello Client'}), 并定义您的网络方法,例如:- [网络方法] 公共静态字符串GetArray(string Msg) {

返回消息;

}

您必须在 ajax 调用中传入数据参数的方法参数名称相同。

你可以在下面看到这个例子:-

        $(document).ready(function() {
    debugger;
    $.ajax({
        type: "POST",
        url: "flight-result-online.aspx/GetArray",
        data: JSON.stringify({ title: 'MP3', songname: 'Gulabi Ankhe' }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        },
        error: function(msg) {
            alert(msg.d);
        }
    });

});

您的 Web 方法定义如下:-

[WebMethod]
public static string GetArray(string title, string songname)
{

return title+" "+songname;

}

【讨论】:

  • .NET WebMethod 为什么要添加“d”节点作为顶层?看起来很奇怪……有办法改变吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2010-10-18
  • 1970-01-01
相关资源
最近更新 更多