简体   繁体   English

Ajax调用不起作用

[英]Ajax call not working

I am making an ajax call to C# function but it is not being call. 我正在对C#函数进行ajax调用,但它没有被调用。

This is ajax call: 这是ajax调用:

$('#button1 button').click(function () {
    var username = "username_declared";
    var firstname = "firstname_declared";
    $.ajax({
        type: "GET",
        url: "practiced_final.aspx/ServerSideMethod",
        data:{username1:username,firstname1:firstname},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $('#myDiv').text(msg.d);
        },
        error: function (a, b, c) {
            alert(a + b + c);
        }
    });
    return false;
});

This is C# code: 这是C#代码:

[WebMethod]
public static string ServerSideMethod(string username1, string firstname1)
{
    return "Message from server with parameter." + username1 + "hello" + firstname1;
}

This method is not getting hit and shows a error message like this: 此方法未被命中,并显示如下错误消息:

object XMLHttpRequest]parsererrorundefined

Any help will be highly appreciated. 任何帮助将受到高度赞赏。

$('#button1 button').live('click', function () {
            var username = "username_declared";
            var firstname = "firstname_declared";
            $.ajax({
                url: "/practiced_final.aspx/ServerSideMethod", type: "GET", dataType: "json",
                data: JSON.stringify({ username1: username, firstname1: firstname }),
                contentType: "application/json; charset=utf-8",
                success: function (msg) {
                    $('#myDiv').text(msg.d);
                },
                error: function (a, b, c) {
                    alert(a + b + c);
                }
            });
        });

$('button#button1') or $('#button1') or $('#button1 button') check u selector also. $('button#button1')或$('#button1')或$('#button1 button')也检查你的选择器。 put one alert message inside click event and see 在Click事件中放入一条警报消息并查看

Finally it is working.This is the final code. 最后它正在工作。这是最终的代码。 Thanks everyone for your wise replies. 谢谢大家的明智回复。

                $.ajax({
                 type: "POST",
                 url: "practiced_final.aspx/hello_print",
                 data: "{}",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: true,
                 cache: false,
                 success: function (msg) {
                 $('#myDiv').text(msg.d);
                 }
                 })
                 return false;

Enjoy. 请享用。

Try changing this line: 尝试更改此行:

data:{username1:username,firstname1:firstname},

to

data:JSON.stringify({username1:username,firstname1:firstname}),

Edit: 编辑:

I'm not sure if this is the cause of the problem, but this is one thing I noticed different between our jQuery ajax calls. 我不确定这是否是问题的原因,但我注意到我们的jQuery ajax调用之间有一点不同。 Also, changed result string to reflect @dana's criticism in the comments of my answer. 另外,改变了结果字符串以反映@ dana在我的回答评论中的批评。

In your code I can see : dataType: "json", 在你的代码中我可以看到:dataType:“json”,

But you're not sending a Json through your C# function... When you're telling ajax that the dataType is a json, it will JSON.parse() the response. 但是你没有通过你的C#函数发送一个Json ...当你告诉ajax dataType是一个json时,它会响应JSON.parse()。 Maybe that's where the failure is. 也许这就是失败的地方。 Try changing the dataType, or removing it (jQuery will try to guess it). 尝试更改dataType,或删除它(jQuery会尝试猜测它)。

  $('button#button1') //Or check selector put one alert inside onclick
  $('#button1').live('click', function () {
                var username = "username_declared";
                var firstname = "firstname_declared";
                $.ajax({
                    type: "GET",
                    url: "practiced_final.aspx/ServerSideMethod",
                    data: JSON.stringify({ username1: username, firstname1: firstname }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                    $('#myDiv').text(msg.d);
                    },
                    error: function (a, b, c) {
                        alert(a + b + c);
                    }
                    })
                    return false;

it may help 它可能有所帮助

Please Change below line in Jquery function. 请在Jquery函数中更改以下行。

data:{username1:username,firstname1:firstname}, 数据:{USERNAME1:用户名,FIRSTNAME1:姓名},

to

data:"{'username1':'" + username + "','firstname1':'" + firstname + "'}", 数据:“{'username1':'”+ username +“','firstname1':'”+ firstname +“'}”,

Hope this will help you. 希望这会帮助你。

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

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