简体   繁体   English

服务器未从ajax调用接收数据

[英]Server does not receive data from ajax call

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. 我正在尝试通过ajax调用发送textarea的内容,但是它似乎没有用,我也不知道为什么。

There's the method called GetStatus(string statusText) which need to receive the content. 有一种称为GetStatus(string statusText)的方法,该方法需要接收内容。

Here's the javascript code: 这是JavaScript代码:

$("#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. 您还应该知道,我是Web开发的新手。

  1. You can't have a request body in a GET request, you have to use a POST request for that 您不能在GET请求中包含请求正文,而必须为此使用POST请求
  2. The string you are constrcting is not valid JSON since: 您正在构造的字符串不是有效的JSON,因为:
    • Property names must be strings 属性名称必须是字符串
    • You have no idea what the user will enter in the textarea - it might contain characters with special meaning in JSON 您不知道用户将在文本区域中输入什么-它可能包含JSON中具有特殊含义的字符

Generate your JSON programatically. 以编程方式生成JSON。

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

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. 显然,还需要将流程的服务器端设置为接受带有JSON主体(而不是更标准的URL表单编码格式)的POST请求。

Try this: 尝试这个:

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

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

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

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