服务器不从ajax调用接收数据 [英] Server does not receive data from ajax call

查看:76
本文介绍了服务器不从ajax调用接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题。我正在尝试使用ajax调用发送textarea的内容,但它似乎没有工作,我不知道为什么。

I have a problem. I'm trying to send content of a textarea with an ajax call, but it doesn't seem to be working, and I don't know why.

有一种名为 GetStatus(string statusText)的方法需要接收内容。

There's the method called GetStatus(string statusText) which need to receive the content.

以下是javascript代码:

Here's the javascript code:

$("#btnSaveStatus").on("click", function () {
                    var statusText = $(".textareaEdit").val();

                    $.ajax({
                        type: "GET",
                        url: "Default.aspx/GetStatus",
                        data: "{statusText:'" + statusText + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (result) {
//                            $('#littlbioID').text(result.d);
                        }
                    });
                });

请告知。你也应该知道我是网络开发的新手。

Please advise. You should also know that I'm new into web development.

推荐答案


  1. 你不能拥有GET请求中的请求正文,您必须使用POST请求

  2. 您构建的字符串无效JSON,因为:

    • 属性名称必须是字符串

    • 您不知道用户将在textarea中输入什么内容 - 它可能包含JSON中具有特殊含义的字符

以编程方式生成JSON。

Generate your JSON programatically.

{
  type: "POST",
  url: "Default.aspx/GetStatus",
  data: JSON.stringify({
    statusText: statusText
  }),
  // etc

显然,服务器端的流程需要设置为接受带有JSON正文的POST请求(而不是更标准的URL表单编码格式)。

Obviously, the server side of the process needs to be set up to accept a POST request with a JSON body (instead of the more standard URL Form Encoded format) as well.

这篇关于服务器不从ajax调用接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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