【问题标题】:changing from GET to POST jquery ajax call to c#从 GET 更改为 POST jquery ajax 调用到 c#
【发布时间】:2011-02-21 08:48:31
【问题描述】:

我试图使用 GET 并遇到了麻烦。现在我正在尝试切换到 POST 来尝试一下。我从

切换了我的 jquery 语句
Type: GET,

Type: POST,

我将 .asmx 代码切换到

[WebMethod(EnableSession = true)]
[ScriptMethod]
public string testGetParametersDynamic(string firstName, string lastName)
{
    string fullName = firstName + lastName;

    return fullName;
}

来自

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public string testGetParametersDynamic(string firstName, string lastName)
{
    string fullName = firstName + lastName;

    return fullName;
}

我现在收到此错误:

尝试使用 GET 请求调用方法 \testGetParametersDynamic\u0027

我错过了什么?

【问题讨论】:

  • 在 JS 中发布调用的完整代码可能是个好主意。
  • @spender: +1; @dan_vitch:您还可以使用 firefox 的 firebug 扩展检查请求类型。

标签: c# jquery web-services


【解决方案1】:

为服务添加以下属性

[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService

编辑

查看以下代码。它工作正常

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $("#btnTest").click(function () {
                var firstName = "a";
                var lastName = "b";
                $.ajax({
                    type: "POST",
                    data: "{firstName:'" + firstName + "',lastName:'" + lastName + "'}",
                    url: "WebService.asmx/testGetParametersDynamic",
                    contentType: "application/json",
                    dataType: "json",
                    success: function (ret) {
                        alert(ret);
                    }


                }

                );
            });
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" name="name" value="Call " id="btnTest" />
    </div>
    </form>
</body>
</html>

WebService.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService
{

    public WebService()
    {


    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public string testGetParametersDynamic(string firstName, string lastName)
    {
        string fullName = firstName + lastName;

        return fullName;
    }

}

【讨论】:

    【解决方案2】:

    我认为你必须使用:

    [ScriptMethod(UseHttpGet = false)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2012-01-23
      • 2018-01-10
      • 2013-03-03
      • 2017-11-09
      • 2012-05-29
      • 2022-01-20
      相关资源
      最近更新 更多