简体   繁体   English

尝试对 Web 表单方法进行 Ajax 调用并收到内部服务器错误。 我已经尝试了几乎所有类似问题的解决方案

[英]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 对象,其中keyDatavalueData具有相应的值。

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.

相关问题 Laravel Ajax 表单发布显示 500 内部服务器错误。 我尝试了很多解决方案但仍然无法解决 - Laravel Ajax form post shows 500 internal server error. I tried solving with a lot of solution but still not working jQuery Ajax POST不能动态添加输入,输入具有名称,而我尝试了其他解决方案 - Jquery Ajax POST is not getting dynamically added inputs inputs have names and I have tried other solutions 尝试使类似功能使用Ajax后出现内部服务器错误500 - Getting internal server error 500 after trying to make the like functionality use Ajax 从 Ajax 调用 .Net 中的 SOAP Web 方法获取 500 服务器错误 - Getting 500 server error from Ajax call to a SOAP web method in .Net Laravel - Ajax 调用上的 500(内部服务器错误) - Laravel - 500 (Internal Server Error) on Ajax Call Ajax调用返回500内部服务器错误 - Ajax call returning 500 internal server error Ajax调用发生内部服务器错误 - ajax call occuring internal server error Ajax和PHP表单-500(内部服务器错误) - Ajax and PHP form - 500 (Internal Server Error) Drupal:Ajax发布获取内部服务器错误 - Drupal: Ajax post getting internal server error 尝试在Visual Studio中从JavaScript调用插入值到SQL Server时出现内部服务器错误500 - Getting a internal server error 500 while trying to call insert values from javascript into sql server in visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM