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.

    评论

报告相同问题?