我如何发送JSON但希望使用AJAX / JQuery响应纯文本?

时间:2022-01-14 20:16:09

In the following script, I post data in JSON format to the server, but the response is sent back in plain-text.

在以下脚本中,我将JSON格式的数据发布到服务器,但响应以纯文本形式发回。

    var regCredentials = {

        "username": creds.username,
        "password": creds.password,
        "fname": creds.fname,
        "lname": creds.lname

    };

    request = $.ajax({

        url: "internet.com/register",
        type: "POST",
        crossDomain: true,
        data: regCredentials,
        dataType: "json";

        request.always(function (data) {

                console.log("Response: " + data);
                postResponse(data);

        };

    });

Because the function expects JSON back, a " is appended to the returned data. It's probably parsed too, which doesn't throw an error for some reason.

因为函数需要返回JSON,所以“会将”附加到返回的数据。它可能也被解析了,因为某种原因不会抛出错误。

How can I write a JQuery AJAX Post that accepts a response in plaintext format?

如何编写以纯文本格式接受响应的JQuery AJAX帖子?

Edit:

编辑:

My confusion I realize, is that I thought data determined the outgoing and incoming expected format rather than just incoming. Well that doesn't make much sense does it? Thanks for the answer!

我意识到,我的困惑在于,我认为数据确定了传出和传入的预期格式,而不仅仅是传入。嗯这没有多大意义呢?感谢你的回答!

1 个解决方案

#1


5  

The dataType parameter is used to set the expected response type. In your case, set it to text. This does not affect the information which is sent in any way. As you are sending a POST request, the information will be placed in the requests' header. If it was a GET, it would be serialised to a string and appended to the URL in the querystring.

dataType参数用于设置预期的响应类型。在您的情况下,将其设置为文本。这不会影响以任何方式发送的信息。当您发送POST请求时,信息将放在请求的标头中。如果它是GET,它将被序列化为一个字符串并附加到查询字符串中的URL。

Your syntax around the handler function was a little off too. Try this:

你在处理函数周围的语法也有点过时了。尝试这个:

var regCredentials = {
    "username": creds.username,
    "password": creds.password,
    "fname": creds.fname,
    "lname": creds.lname
};

request = $.ajax({
    url: "internet.com/register",
    type: "POST",
    crossDomain: true,
    data: regCredentials,
    dataType: "text",
    success: function (data) {
        console.log("Response: " + data);
        postResponse(data);
    }
});

#1


5  

The dataType parameter is used to set the expected response type. In your case, set it to text. This does not affect the information which is sent in any way. As you are sending a POST request, the information will be placed in the requests' header. If it was a GET, it would be serialised to a string and appended to the URL in the querystring.

dataType参数用于设置预期的响应类型。在您的情况下,将其设置为文本。这不会影响以任何方式发送的信息。当您发送POST请求时,信息将放在请求的标头中。如果它是GET,它将被序列化为一个字符串并附加到查询字符串中的URL。

Your syntax around the handler function was a little off too. Try this:

你在处理函数周围的语法也有点过时了。尝试这个:

var regCredentials = {
    "username": creds.username,
    "password": creds.password,
    "fname": creds.fname,
    "lname": creds.lname
};

request = $.ajax({
    url: "internet.com/register",
    type: "POST",
    crossDomain: true,
    data: regCredentials,
    dataType: "text",
    success: function (data) {
        console.log("Response: " + data);
        postResponse(data);
    }
});