Lucas S. Müller Lucas S. Müller - 1 month ago 4
Javascript Question

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.

My jQuery code is:

$(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
.

enter image description here

Answer

Please use code as below

Because you are using text data type from query string, you can make datatype as 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");
                }
            });
        });