AngularJS:$http.get 405(不允许的方法) [英] AngularJS: $http.get 405 (Method Not Allowed)

查看:35
本文介绍了AngularJS:$http.get 405(不允许的方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我收到来自 url 的请求时,如下所示:

<前>$http({方法:'获取',网址:'http://mooc-lms.dev.web.nd/v0.3/users/login'}). 成功(功能(数据,状态,标题,配置){//代码});

但我得到的错误是:

<前>GET http://mooc-lms.dev.web.nd/v0.3/users/login 405(不允许的方法)

但是,如果我将方法从GET"更改为POST",则错误是:

<前>POST http://mooc-lms.dev.web.nd/v0.3/users/login 415(不支持的媒体类型)

有什么问题吗?url有问题吗(http://mooc-lms.dev.web.nd/v0.3/users/login)?我在 url 中发现 '"message":"Request method 'GET' not supported"'.

解决方案

GET 不工作的原因是服务器不支持 GET登录端点,这并不奇怪.在 POST 请求上获得 HTTP 415 响应的最常见原因是因为服务器要求您指定 Content-Type 和/或Accept 在您的请求标头中.

我下面的示例将它们设置为 application/json,这很常见,但并不普遍,因此您必须检查服务器需要什么,以及它会给您什么.鉴于该地址包含mooc-lms",我假设您正在学习某种在线课程.它应该给你这些信息.该文档还应告诉您需要使用 data 属性发送哪些数据.

$http({方法:'POST',网址:'http://mooc-lms.dev.web.nd/v0.3/users/login',标题:{'Content-Type': 'application/json',/* 或任何相关的类型 */'接受':'应用程序/json'/* 同上 */},数据: {/* 如果你打算登录,你可能需要发送一些数据 */}})

when I get a request from a url,like this:


    $http({
        method: 'GET',
        url:'http://mooc-lms.dev.web.nd/v0.3/users/login'
    }).success(function(data, status, headers, config) {
        //code
    });

But the error I get is:


    GET http://mooc-lms.dev.web.nd/v0.3/users/login 405 (Method Not Allowed)

However,if I change the method from "GET" to "POST",the error is:


    POST http://mooc-lms.dev.web.nd/v0.3/users/login 415 (Unsupported Media Type)

What's the problem?Is there something wrong with the url(http://mooc-lms.dev.web.nd/v0.3/users/login)? I find '"message":"Request method 'GET' not supported"' in the url.

解决方案

The reason that GET isn't working is that the server doesn't support GET for the login endpoint, which is unsurprising. The most common reason for getting an HTTP 415 response on a POST request is because the server requires you to specify a Content-Type and/or Accept in your request header.

My example below sets them to application/json, which is common, but not ubiquitous, so you'll have to check what the server requires, and what it will give you back. Given that the address contains "mooc-lms", I assume you're doing some kind of online course. It should give you that information. That documentation should also tell you what data you need to send with the data property.

$http({
    method: 'POST',
    url: 'http://mooc-lms.dev.web.nd/v0.3/users/login',
    headers: {
        'Content-Type': 'application/json', /*or whatever type is relevant */
        'Accept': 'application/json' /* ditto */
    },
    data: {
        /* You probably need to send some data if you plan to log in */
    }
})

这篇关于AngularJS:$http.get 405(不允许的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆