The Json.stringify__json of Ajax requests

Source: Internet
Author: User

issue: to ensure that the background program is not a problem, through the postman can successfully submit the form, but from the front page with Ajax submitted but always execute the error callback function, debug, found that postman sent the request in the format of the data in JSON, But the debug front page sends a string of Attribute1=value1&attribute2=value2&. The problem is where.

Background interface:

/*controller request url*/
var url = "${ctx}/developer/register";
/* Contract Data Format */
Consume=mediatype.application_json_value

JSON data:

Json_data = {
                "loginName": loginName,
                "password": Password1,
                "name": Name,
                "email": Email,
                " Sex ": Sex,
                " Avatar ": Avatar,
                " description ": Description
            }

Ajax Request Code-mode one:

<span style= "line-height:1.5;" >$.ajax ({
    type: "POST",
    Url:url,
    contentType: "Application/json",
    dataType: "JSON",
    data: Json_data,
    success:function () {
          Window.alert ("Add developer succeeded");
    },
    error:function (XMLHttpRequest) {
          Alert ("Request error:" +xmlhttprequest.status+xmlhttprequest.statustext);
    }
 }); </span>

Ajax request Result: Each time is execution error, error code: 400,bad request.

Ajax Request Code-mode two:

$.ajax ({
    type: "POST",
    Url:url,
    contentType: "Application/json",
    dataType: "JSON",
    data: Json_data,
    success:function () {
          Window.alert ("Add developer succeeded");
    },
    error:function (XMLHttpRequest) {
          Alert ("Request error:" +xmlhttprequest.status+xmlhttprequest.statustext);
    }
 });

Ajax Request Results: Pop-up "Add Developer Success" dialog box, solve the problem.

Reason: Controller and foreground conventions deliver JSON-formatted data, so the foreground Ajax must pass the JSON-formatted data, and by looking at the documentation it is known that the Data property receives a string of parameter types, Therefore, you use Json.stringify to convert the JSON object to a string type.

Attached >>jquery Ajax method Link: http://www.w3cschool.cn/ajax_ajax.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.