[英]Trying to make an Ajax call to a web form method and getting an internal server error. I have tried almost all solutions to similar questions
The function getClientData() gets called from one of the anchor tags in a grid's column.从网格列中的锚标记之一调用函数 getClientData()。 The anchor tag has a couple of Data-Tags which are passed to the code behind method.
锚标签有几个数据标签,它们被传递给代码隐藏方法。 I have to perform some DB operation through these parameters but before that I wanted to make sure if this prototype would work.
我必须通过这些参数执行一些数据库操作,但在此之前我想确定这个原型是否可以工作。
This is how the anchor tag looks:这是锚标记的外观:
<a href="JavaScript:void(0);" onclick="getClientData()" class="endec" data-kt="Cpuqsrtsotmfegrhsi-jikdbCvuwsxtcodmeelrmI-Dn-ovpcqSresctrfegthKiejy" data-kv="1p7q9">Show</a>
This is my Javascript method:这是我的 Javascript 方法:
function getClientData() {
//var dataValue = { "keyData": this.event.target.getAttribute("data-kt"), "valueData": this.event.target.getAttribute("data-kv")};
$.ajax({
type: "GET",
url: "Clients.aspx/GetClientData",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ keyData: this.event.target.getAttribute("data-kt"), valueData: this.event.target.getAttribute("data-kv")}),
dataType: 'json',
error: function (error) {
alert(error.statusText);
},
success: function (result) {
alert("Success: " + result );
}
});
}
I put a break point here and it never gets triggered.我在这里设置了一个断点,它永远不会被触发。 This is my web method in the code behind file:
这是我在代码隐藏文件中的网络方法:
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public string GetClientData(string keyData, string valueData)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(keyData) && !string.IsNullOrEmpty(valueData))
{
result = "Decrypted String!";
}
return result;
}
This is the URL that gets created for the POST request " http://localhost:60825/Clients.aspx/GetClientData ?{%22keyData%22:%22Cpuqsrtsotmfegrhsi-jikdbCvuwsxtcodmeelrmI-Dn-ovpcqSresctrfegthKiejy%22,%22valueData%22:%221p7q9%22}".这是为 POST 请求创建的 URL “ http://localhost:60825/Clients.aspx/GetClientData ?{%22keyData%22:%22Cpuqsrtsotmfegrhsi-jikdbCvuwsxtcodmeelrmI-Dn-ovpcqSresctrfegthKiejy%21%22%21%21%22%22%220 %22}”。 Please let me know if I am doing something wrong.
如果我做错了什么,请告诉我。
I am not sure how you configured routing but based on your API method (code behind) your data should be formatted in following manner:我不确定您如何配置路由,但根据您的 API 方法(代码隐藏),您的数据应按以下方式格式化:
Method 1 :方法一:
http://localhost:60825/Clients.aspx/GetClientData?keyData=Cpuqsrtsotmfegrhsi-jikdbCvuwsxtcodmeelrmI-Dn-ovpcqSresctrfegthKiejy&valueData=1p7q9
As you can see, instead passing stringified JSON object I am sending data in format of query string where keyData
and valueData
has corresponding values.如您所见,我以查询字符串格式发送数据,而不是传递字符串化的 JSON 对象,其中
keyData
和valueData
具有相应的值。
Method 2 :方法二:
If you prefer to send stringified JSON you can modify your Payload in URL like this:如果您更喜欢发送字符串化的 JSON,您可以像这样修改 URL 中的 Payload:
http://localhost:60825/Clients.aspx/GetClientData?data=%7BkeyData%22%3A%22Cpuqsrtsotmfegrhsi-jikdbCvuwsxtcodmeelrmI-Dn-ovpcqSresctrfegthKiejy%22%2C%22valueData%22%3A%221p7q9%22%7D
Here I am sending stringified JSON as data
parameter in query string.在这里,我将字符串化的 JSON 作为查询字符串中的
data
参数发送。 For that purpose your code behing method needs to be like this:为此,您的代码 beh 方法需要是这样的:
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public string GetClientData(string data)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(keyData) && !string.IsNullOrEmpty(valueData))
{
result = "Decrypted String!";
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.