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",
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'