jquery - 我在一项考试中遇到 ajax 调用问题

标签 jquery ajax json

我在考试时遇到了这个问题。有人可以帮忙吗?在我的研究中,我发现 dataType 类似于“json”或“xml”,而不是确切的 mime 类型。 另一方面接受使用文字对象来定义 mime 类型。(根据 this 判断)。类似于:

$.ajax({
    url: ...
    dataType: 'json',
    accepts: {
        xml: 'text/xml',
        text: 'text/plain'
    }
});

内容类型适用于

When sending data to the server, use this content type.

来自 jQuery 文档。

如果有人能帮助解决这个问题那就太好了。谢谢。

考试问题:

You are developing a web application that retrieves data from a web service. The data being retrieved is a custom binary datatype named bint. The data can also be represented in XML. Two existing methods named parseXml() and parseBint() are defined on the page.

The application must: ? Retrieve and parse data from the web service by using binary format if possible ? Retrieve and parse the data from the web service by using XML when binary format is not possible

You need to develop the application to meet the requirements. What should you do? (To answer, drag the appropriate code segment to the correct location. Each code segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.)

代码:

var request = $.ajax({
    uri: '/',

选项 1:接受:'application/bint,text/xml',

选项 2:contentType:'application/bint,text/xml'

选项 3:数据类型:'application/bint,text/xml'

    dataFilter: function(data, type) {

选项 1:if(request.getResponseHeader("Content-Type"== 'application/bint')

选项 2:if(type == 'application/bint')

选项 3:if(request.mimeType == 'application/bint')

    },
    success: function(data) {
        start(data);
    }
});

最佳答案

我认为这里的关键是这一点:

The data being retrieved is a custom binary datatype named bint.

这意味着您期待 bint,而不是发送 bint。因此,这里的答案是接受

第二部分:

  1. type 不是 MIME 类型,它是一个字符串(来源 here )
  2. request.mimeType 不是 XmlHttpRequest 的有效属性(来源 here )

因此答案是 request.getResponseHeader("Content-Type") == 'application/bint' (来源 here )

关于jquery - 我在一项考试中遇到 ajax 调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29103455/

相关文章:

javascript - 如何构建 jquery/javascript 来完成像 SO 的 "Confirm Navigation?"这样的事件/警报

java - 如何访问Docker容器中存储在 'src/main/resources/someFolder'中的应用程序资源文件?

javascript - 如何将此 javascript/jQuery 转换为 "not inline"函数?

javascript - jQuery 周选择器

javascript - core-ui-select 重写 URL 后不起作用

java - 如何在 Android 中使用 WordPress 的 WTI Like Post?

javascript - 根据时间更新div内容

java - 发送对象时忽略 JSON 字段(反序列化)

ruby-on-rails - rails jbuilder : how to build a JSON from an array with the key equals to the array values

javascript - 除以 anchor 字符串值?