Server does not receive data from ajax call

后端 未结 2 747
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 13:21

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.

There\'s the method called

相关标签:
2条回答
  • 2021-01-27 13:55

    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);
                            }
                        });
                    });
    
    0 讨论(0)
  • 2021-01-27 14:03
    1. You can't have a request body in a GET request, you have to use a POST request for that
    2. The string you are constrcting is not valid JSON since:
      • 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

    Generate your JSON programatically.

    {
      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.

    0 讨论(0)
提交回复
热议问题