简体   繁体   English

Ajax POST永远不会触发,既不会成功也不会出错

[英]Ajax POST never gets triggered, neither success nor error

What I am trying to do by using AJAX is when I click "Select" Button, all the related information coming from SQL DB will be assigned to the related labels. 我要使用AJAX尝试做的是,当我单击“选择”按钮时,来自SQL DB的所有相关信息都将分配给相关标签。 The problem is that the AJAX code below($.ajax) never gets triggered (neither success nor error). 问题在于,下面的AJAX代码($ .ajax)永远不会被触发(成功或错误)。 alert(res[0]) is showing the true result on screen (so the clientid parameter is true). alert(res [0])在屏幕上显示真实结果(因此clientid参数为true)。

This is from a WebApplication Project in Asp.Net 这是来自Asp.Net中的WebApplication项目

       function BindClientSummaryForm() {
            var skillsSelect = document.getElementById('<%= ddlSFoundClients.ClientID %>');
            var selectedText = skillsSelect.options[skillsSelect.selectedIndex].text;
            var res = selectedText.split('-');
            document.getElementById('<%= hiddenClientID.ClientID %>').value = res[0];
            alert(res[0]);
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetClientSummaryData",
                contentType: "application/json;charset=utf-8",
                data: "{ 'clientid': " + res[0] + "}",
                dataType: "json",
                success: function (data) {
                    alert("deneme");
                    document.getElementById('<%= lblClientNameSurnameD.ClientID %>').innerHTML = data.d.Firstname + " " + data.d.Lastname;
                    document.getElementById('<%= lblDateOpenedD.ClientID %>').innerHTML = data.d.DateFileOpened;
                    document.getElementById('<%= lblCityD.ClientID %>').innerHTML = data.d.City;
...
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Error occured while filling ClientSummary part.");
                }
            });
            return false;
        }
<asp:Button runat="server" CssClass="btnSelect fieldButton" ID="btnSSelect" Text="Select"  OnClientClick="return BindClientSummaryForm()"></asp:Button>

CodeBehind 代码背后

[WebMethod]
public static MyClient GetClientSummaryData(String clientid) //GetData function
{
   ...
   return client;
}

the debugger never drops to the GetClientSummaryData(clientid) method in c# as well. 调试器也绝不会使用c#中的GetClientSummaryData(clientid)方法。

I appreciate for your helps. 感谢您的帮助。

Try to change the value of content-type to text/plain on both, the client and the server , and use eval ('('+data+')') in the success function. 尝试在客户端服务器上都将content-type的值更改为text/plain ,并在成功函数中使用eval ('('+data+')')

I know that this is not correct because the server response is json, but happened to me the same thing and I discovered (i don't know the reason) when you return the content as content-type: application/json on the server, jQuery does not call the success function. 我知道这是不正确的,因为服务器响应是json,但是我也遇到了同样的事情,当您将内容返回为content-type: application/json时,我发现(我不知道原因) content-type: application/json服务器上的content-type: application/json , jQuery不调用成功函数。

What for do you specify POST as a request type? 您将POST指定为请求类型是什么? It seems like it will be more natural if you use GET (even method name tells us about that). 如果您使用GET,这似乎会更自然(即使方法名称也告诉我们)。

Also I'm not sure about this syntax data: "{ 'clientid': " + res[0] + "}" because I prefer to write scripts in separate files. 另外,我不确定以下语法data: "{ 'clientid': " + res[0] + "}"因为我更喜欢在单独的文件中编写脚本。 Anyway, you can try to write data: { clientid: res[0] } instead 无论如何,您可以尝试写入data: { clientid: res[0] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM