weixin_33716557 2016-02-03 20:50 采纳率: 0%
浏览 36

Ajax请求-xml / json

Studying for an exam and came across the following practice question.

You develop a web application by using jQuery. You develop the following jQuery code:

$(document).ready(function () {
            $('#submit').click(function () {
                $.ajax({
                    //INSERT CODE
                    data: $('#myForm').serialize(),
                    success: function (result) {
                        $('#result').text(result.message);
                    }
                });
            })
        })

The web application exposes a RESTful web api that has an endpoint of product/create. You need to create a new product by using AJAX. Which code segment should you insert at line 04?

                //Option A:
                type: "POST",
                dataType: "xml",
                contentType: "application/x-www-urlencoded; charset=UTF-8",
                url: ".product/create",

                //OPTION B:
                type: "POST",
                dataType: "json",
                url: ".product/create",

Could someone explain why option B is correct? I understand that it should be a post request since a new product is being created. Datatype could be either json or xml. Content-type is optional. Is it because result.message can only work when a json is passed in?

  • 写回答

1条回答 默认 最新

  • weixin_33739646 2016-02-03 21:12
    关注

    For the datatype:"xml", The contenttype is not valid in Option A. The valid options for XMLs are: text/xml, application/xml.

    But, Option B has valid entries.

    The corrected option A is below,

    //Option A:
    type: "POST",
    dataType: "xml",
    contentType: "application/xml; charset=UTF-8",
    url: ".product/create",
    
    评论

报告相同问题?