Javascript 使用fetch和ajax获取json会产生不同的响应

Javascript 使用fetch和ajax获取json会产生不同的响应,javascript,json,fetch,Javascript,Json,Fetch,我一直在使用ajax获取一些json数据,最近尝试使用该实现。 我有不同的响应,我的ajax返回一个包含所有键/值对的字符串,而fetch查询返回的响应对象根本不包含任何键/值对。(我在两个示例中请求完全相同的资源,并收到不同的响应) 谁能告诉我我做错了什么或者为什么会这样 ajax请求: 控制台日志结果:(这是我希望使用fetch方法得到的响应) [id:1,“姓名”:“二对二”,“缩写”:“2v2”,“内部”:真,“长度”:50,“容量”:1,“价格”:“50.2”,“工资”:“15.22”

我一直在使用ajax获取一些json数据,最近尝试使用该实现。 我有不同的响应,我的ajax返回一个包含所有键/值对的字符串,而fetch查询返回的响应对象根本不包含任何键/值对。(我在两个示例中请求完全相同的资源,并收到不同的响应)

谁能告诉我我做错了什么或者为什么会这样

ajax请求:

控制台日志结果:(这是我希望使用fetch方法得到的响应)

[id:1,“姓名”:“二对二”,“缩写”:“2v2”,“内部”:真,“长度”:50,“容量”:1,“价格”:“50.2”,“工资”:“15.22”,“url”:“},{”id:2,“姓名”:“三节课”,“缩写”:“3SUM”,“内部”:假,“长度”:50,“容量”:3,“价格”:“33.33”,“工资”:“11.11”,“url”:“},{”id:3,“姓名”:“Prod1”,“缩写”:“prum”,“内部”:真,“长度”:22,“容量”:2,“价格”:“20.0”,“工资”:“20.0”,“url”:“}]

获取请求:

控制台日志结果:(响应一个充满其他对象的对象,但似乎只是一个html响应,没有我请求的任何数据)

响应{} 正文:(……) bodyUsed:假 标题:标题 好的,没错 现状:200 状态文本:“确定” 类型:“基本” 网址:“ 原型:响应

我还在控制台中使用fetch方法接收到一个错误,该错误说明如下:**


SyntaxError:意外标记不确定,可能将数据类型“text”更改为“json”

fetch需要一个参数来验证其请求:

凭据:“相同来源”


为了清楚起见,我希望在ajax示例中得到相同的响应,但我希望使用我的fetch查询得到相同的响应。谢谢您的回答。:)
$.ajax({
    url: "/" + name + ".json",
    dataType: "text",
    success: function (data) {
        console.log(data);
        var json = $.parseJSON(data);
        var itemArray = [];
        $.each(json, function() {
                itemArray.push( { value: this.id, label: this.name } );
        });
        //Populate the List
        populateListBox(name, itemArray);
    }
});
fetch("/" + name + ".json")
    .then(function(response) {
        console.log(response);
        return response.json()
    }).then(function(json) {
        var itemArray = populateItemArray(this, json);
        populateListBox(name, itemArray);
    }).catch(function(ex) {
        console.log('parsing failed', ex)
    });
fetch("/" + name + ".json", **{ credentials: 'same-origin' }**)
        .then(function(response) {
            return response.json()
        }).then(function(json) {
            var itemArray = populateItemArray(json);
            itemArray = sortByLabel(itemArray, 'label');
            populateListBox(name, itemArray);
        }).catch(function(ex) {
            console.log('parsing failed', ex)
        });