简体   繁体   English

jQuery的AJAX调用过程中的问题

[英]c# - Problems during AJAX call with Jquery

Good evening, I'm trying to do a AJAX call in a C# page and I'm dealing with some problems. 晚上好,我正在尝试在C#页面中进行AJAX调用,并且正在处理一些问题。

My jQuery code is: 我的jQuery代码是:

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "conteudo.aspx/GetNewPost",
        data: { ids: "<%=Request.QueryString["idconteudo"]%>" },
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (data) {
            alert("ok");
        }
    });
});

And my code-behind is: 我的后台代码是:

[WebMethod]
    public static string GetNewPost(string ids)
    {
        // some code
        return ids;
    }

Does someone know what is going on? 有人知道发生了什么吗? PS: The error is Internal Server Error . PS:错误是Internal Server Error

在此处输入图片说明

try: 尝试:

var idconteudo = "<%=Request.QueryString["idconteudo"]%>"; var idconteudo =“ <%= Request.QueryString [” idconteudo“]%>”;

... ...
... ...
url: "conteudo.aspx/GetNewPost", 网址:“ conteudo.aspx / GetNewPost”,
data: "{ids: \\"" + idconteudo + "\\"" }", 数据:“ {ids:\\”“ + idconteudo +” \\“”}“,
contentType: 'application/json; contentType:'application / json; charset=utf-8', charset = utf-8',
... ...
... ...

Please use code as below 请使用以下代码

Because you are using text data type from query string, you can make datatype as text 因为您正在使用查询字符串中的文本数据类型,所以可以将数据类型设置为text

$(document)
        .ready(function () {
            var q = "<%=Request.QueryString["idconteudo"]%>";
            alert(q);// just to check the value
            // assuming that you had passed query string value

            $.ajax({
                type: "POST",
                url: "conteudo.aspx/GetNewPost",
                data: { "ids": q },
                //contentType: 'application/json; charset=utf-8',
                dataType: 'text',// data type should be text
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("Request: " +
                        XMLHttpRequest.toString() +
                        "\n\nStatus: " +
                        textStatus +
                        "\n\nError: " +
                        errorThrown);
                },
                success: function(data) {
                    alert("ok");
                }
            });
        });

Edit 1 : If the Web Method is on ASPX page, you shall use below code, so that you get result in Json format 编辑1 :如果Web方法位于ASPX页面上,则应使用以下代码,以便以Json格式获取结果

$(document)
        .ready(function () {
            var q = "<%=Request.QueryString["idconteudo"]%>";
            //alert(q);
            // just to check the value
            // assuming that you had passed query string value

            $.ajax({
                type: "POST",
                url: "conteudo.aspx/GetNewPost",
                data: '{ids: "' + q + '" }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Request: " +
                        XMLHttpRequest.toString() +
                        "\n\nStatus: " +
                        textStatus +
                        "\n\nError: " +
                        errorThrown);
                },
                success: function (result) {
                    alert("ok" + result.d);
                }
            });
        });

From what you have given us with your post, I see two things that are incorrect and one other thing that may have just not been posted in your question, but it is in fact in your real source. 从您提供给我们的帖子中,我发现有两件事是不正确的,另一件事可能是您的问题中尚未发布的,但实际上这是您的真实资料。

1) Your data is written wrong. 1)您的data写错了。 It should be: 它应该是:

$(document).ready(function () {
    var test = "<%=Request.QueryString["idconteudo"]%>";
    $.ajax({
        type: "POST",
        url: "conteudo.aspx/GetNewPost",
        data: { 'ids' : test }, // the ids needs to have quotes to be correct syntax
        dataType: 'text',
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (data) {
            alert("ok");
        }
    });
});

2) You say it's a POST but your method isn't decorated with the [HttpPost] Annotation. 2)您说这是POST但您的方法未使用[HttpPost]注释修饰。

3) Your dataType is set to 'json' but your method is returning a string .. so 'json' should be changed to 'text' . 3)您的dataType设置为'json'但是您的方法返回一个string ..所以'json'应该更改为'text'

Hope this helps! 希望这可以帮助!

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

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