javascript Ajax/JSON 服务器响应状态为 415(不支持的媒体类型)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9738156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:39:59  来源:igfitidea点击:

Ajax/JSON the server responded with a status of 415 (Unsupported Media Type)

javascriptjqueryajaxjsonlive

提问by Reinard

The current project I'm working on requires me to talk with the windows live API. I use an AJAX request to receive a JSON object with the user details. How ever I keep getting this error: Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)

我正在处理的当前项目需要我与 windows live API 交谈。我使用 AJAX 请求来接收包含用户详细信息的 JSON 对象。我如何不断收到此错误: 无法加载资源:服务器响应状态为 415(不支持的媒体类型)

My first idea was to add a "&callback=?" the url. But then I get "Uncaught SyntaxError: Unexpected token :"in the JSON response.

我的第一个想法是添加一个“&callback=?” 网址。但随后我在 JSON 响应中收到“Uncaught SyntaxError: Unexpected token :”

I've been searching on how I could fix the error(s). But found no working solution for both errors. On top if that I'm quite unsure which error I should try solving (status 415 or unexpected token). Anything pointing me in the right direction would be greatly appreciated!

我一直在寻找如何修复错误。但是没有找到针对这两个错误的有效解决方案。最重要的是,如果我不确定应该尝试解决哪个错误(状态 415 或意外标记)。任何指向我正确方向的东西都将不胜感激!

$.ajax({
        url: "https://apis.live.net/v5.0/me?access_token=" + localStorage.getItem('Live_token'),
        dataType: 'json',
        type : 'GET',
        contentType: "application/json;",
        async: false,
        success : function(data) {
            var id = data.id,
            fname = data.last_name,
            email = data.emails.preferred;//TODO preferred or account?
            alert(id + '|' + fname + '|' + email);
            localStorage.setItem('Live_verifier', id + '|' + fname + '|' + email);
        },
        error : function(data) {
            localStorage.removeItem('Live_token');
        }
    });

thanks in advance

提前致谢

采纳答案by Nicola Peluchetti

Have you tried setting

你有没有试过设置

dataType: 'jsonp',

and the "&callback=?"in the url? You are calling a server on another domain and so you must set jsonpas dataType

"&callback=?"网址中的?您正在调用另一个域上的服务器,因此您必须设置jsonpdataType