weixin_33720956 2015-08-12 08:42 采纳率: 0%
浏览 37

发送Ajax请求ASP.NET

I'm new to the world of ajax requests and asp.net. I'm trying to send an ajax request to aspx page. When I'm debugging the server side it seems to be ok, but the response printing the error message? I tried to change the Response.ContentType but it didn't solve my problem. Any suggestions?

Here is my javascript code:

            function onclick(){
             $.ajax({
                url: "SandaAJAXRequests.aspx",
                type: "post",
                data: JSON.stringify({ "first": "getevent","second":"data" }),
                dataType: 'json',
                success:
            function (response)
            {
                roundNumber = 1;
                numberofblackwins = 0;
                numberofredwins = 0;
                ifBattleIsOver = false;
                $("#round").text(roundNumber);
                var result = response.split(",");
                name1 = result[1];
                name2 = result[2];

                var r = confirm("Is " + name1 + " the black competitor?");
                if (r == true) {
                    $("#black_competitor_name").text(name1);
                    $("#red_competitor_name").text(name2);
                }
                else {
                    $("#black_competitor_name").text(name2);
                    $("#red_competitor_name").text(name1);
                }
            },
                error: function (xhr) {
                    alert("Problem sending data to the server");
                }
            });
        }

Here is my server side:

protected void Page_Load(object sender, EventArgs e)
    {
        string jsonString = String.Empty;
        Request.InputStream.Position = 0;
        using (var inputStream = new StreamReader(Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }
        string data = jsonString.Replace("\\", "");
        char[] seperators = { ':', ',', '"' };
        string[] a = data.Split(seperators);
        string t = a[4];
        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
        object serJsonDetails = javaScriptSerializer.Deserialize(jsonString, typeof(object));
        string requestType = serJsonDetails.ToString();
        switch (t)
        {
            case "getevent":
                SandaEvent current = (SandaEvent)Application["CurrentEvent"];
                Response.ContentType = "text/plain";
                String response = current.id + "," + current.name1 + "," + current.name2;
                Response.Write(response);
                Response.End();
                break;
    }}
  • 写回答

1条回答 默认 最新

  • weixin_33724059 2015-08-12 09:24
    关注

    You are missing contentType in $.ajax({});

    Content-type: application/json; charset=utf-8
    

    is necessary when you are a doing a post using $.ajax({})

    Content-type: application/json; charset=utf-8 designates the content to be in JSON format, encoded in the UTF-8 character encoding.

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt安装后运行不了,这是我电脑的问题吗
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法