ASP.NET的hello world后AJAX [英] ASP.NET hello world AJAX post

查看:158
本文介绍了ASP.NET的hello world后AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到500与下面的C#/ jQuery的。任何给定的执行情况可能并不正确并且那不是一个巨大的问题。我只是想获得一个Hello World起来。它的工作原理如果C#没有参数,但只要我尝试收到的数据它给500。

  [的WebMethod]
公共静态字符串测试(字符串s)
{
    //永远不会在这里
}$阿贾克斯({
    键入:POST,
    网址:ajax.aspx /+方法,
    / *异步:真实,* /
    数据:{数据:'+数据+'},
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON
    成功:功能(数据){
        回调(data.d);
    }
});

最新尝试是这样仍然不工作:

  [的WebMethod()]
公共静态字符串测试(字符串数据)
{
    //永远不会在这里
    返回的Hello World;
}$阿贾克斯({
    键入:POST,
    网址:ajax.aspx /测试,
    数据:数据:{数据:'ABC'},
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON
    成功:功能(数据){
        警报(返回);
    }
});


解决方案

我觉得你不必使用MVC框架,使其工作。我觉得你的方式正在传递JSON参数是错误的。请检查下面code和尝试做让我知道它是否工作。

  [的WebMethod()]
公共静态字符串测试(字符串数据)
{
  //永远不会在这里
  返回的Hello World;
}$阿贾克斯({
    键入:POST,
    网址:ajax.aspx /测试,
    数据:{数据:ABC}
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON
    成功:函数(响应){
      警报(响应);
    }
});

I keep getting a 500 with the following C#/jQuery. Any given implementation may not be right and thats not a huge issue. I'm just trying to get a hello world up. It works if the c# has no arguments but as soon as I try to recieve data it gives the 500.

[WebMethod]
public static string Test(string s)
{
    // never gets here
}

$.ajax({
    type: "POST",
    url: "ajax.aspx/" + method,
    /*async: true,*/
    data: "{data:'" + data + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        callback(data.d);
    }
});

latest attempt is this which still doesnt work:

[WebMethod()]
public static string Test(string data)
{
    // never gets here
    return "hello world";
}

$.ajax({
    type: "POST",
    url: "ajax.aspx/Test",
    data: "data: {data:'abc'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        alert("back");
    }
});

解决方案

I think you don't have to use MVC to make it work. I think the way you are passing json parameters are wrong. Please check below code and try and do let me know whether it works.

[WebMethod()]
public static string Test(string data)
{
  // never gets here
  return "hello world";
}

$.ajax({
    type: "POST",
    url: "ajax.aspx/Test",
    data:'{"data":"abc"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
      alert(response);
    }
});

这篇关于ASP.NET的hello world后AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆