对 ASP.NET 页面方法的 Ajax GET 请求? [英] Ajax GET requests to an ASP.NET Page Method?
问题描述
我本周遇到的一个情况:我们有一个 jQuery Ajax 调用,它返回到服务器以获取数据
A situation I ran across this week: we have a jQuery Ajax call that goes back to the server to get data
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: fullMethodPath,
data: data,
dataType: "json",
success: function(response) {
successCallback(response);
},
error: errorCallback,
complete: completeCallback
});
fullMethodPath
是指向页面上静态方法的链接(比方说 /MyPage.aspx/MyMethod
).
fullMethodPath
is a link to a static method on a page (let's say /MyPage.aspx/MyMethod
).
public partial class MyPage : Page
{
// snip
[WebMethod]
public static AjaxData MyMethod(string param1, int param2)
{
// return some data here
}
}
这行得通,没问题.
一位同事试图用类型为GET"的调用替换此调用.它坏了,我必须修理它.最终,我回到了 POST,因为我们需要快速修复,但它一直困扰着我,因为在这种情况下,GET 在语义上更正确".
A colleague had attempted to replace this call with one where type was "GET". It broke, I had to fix it. Eventually, I went back to POST because we needed the fix quick, but it has been bugging me because semantically a GET is more "correct" in this case.
据我所知,jQuery 将数据中的对象转换为查询字符串:/MyPage.aspx/MyMethod?param1=value1¶m2=value2
但我能得到的只是页面 MyPage.aspx
.
As I understand it, jQuery translates an object in data to a Query String: /MyPage.aspx/MyMethod?param1=value1¶m2=value2
but all I could get back was the content of the page MyPage.aspx
.
这只是 Page 方法的一个特性",还是有一种方法可以使 GET 请求起作用?
Is that just a "feature" of Page methods, or is there a way of making a GET request work?
推荐答案
出于安全原因,ASP.Net AJAX 页面方法仅支持 POST 请求.
For security reasons, ASP.Net AJAX page methods only support POST requests.
这篇关于对 ASP.NET 页面方法的 Ajax GET 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!