我试图使用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
我错过了什么?
答案 0 :(得分:2)
我认为你必须使用:
[ScriptMethod(UseHttpGet = false)]
答案 1 :(得分:2)
将以下属性添加到服务
[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;
}
}