找不到 Angular HTTP 服务 URL 的问题

issue with angular http services url not found

本文关键字:问题 URL 服务 Angular HTTP 找不到      更新时间:2023-09-26

>我正在尝试将数据发布到 api 服务器,但它一直给我 404。我在邮递员中尝试过,效果很好。由于跨域问题,我正在使用 JSONP 发布数据这是控制台显示的内容

GET http://myapi.com/registrations.json 404 (Not Found) angular.js:8227
undefined 0 function (name) {
    if (!headersObj) headersObj =  parseHeaders(headers);
    if (name) {
      return headersObj[lowercase(name)] || null;
    }
    return headersObj;
  } 
Object {method: "JSONP", transformRequest: Array[1], transformResponse: Array[1], url: "http://shipit-integration.herokuapp.com/registrations.json", data: Object…}
data: Object
registration: Object
email: "email@example.com"
password: "pass1234"
__proto__: Object
__proto__: Object
__defineGetter__: function __defineGetter__() { [native code] }
__defineSetter__: function __defineSetter__() { [native code] }
__lookupGetter__: function __lookupGetter__() { [native code] }
__lookupSetter__: function __lookupSetter__() { [native code] }
constructor: function Object() { [native code] }
hasOwnProperty: function hasOwnProperty() { [native code] }
isPrototypeOf: function isPrototypeOf() { [native code] }
propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
get __proto__: function __proto__() { [native code] }
set __proto__: function __proto__() { [native code] }
headers: Object
Accept: "application/json"
Content-Type: "application/json"
__proto__: Object
method: "JSONP"
transformRequest: Array[1]
transformResponse: Array[1]
url: "http://myapi.com/registrations.json"
__proto__: Object

这是我的代码

    var data = {
        "registration": {
            "email": "email@example.com",
            "password": "pass1234"
        }
    };
    var headers =  {
                        'Accept': 'application/json',
                        'Content-Type' : 'application/json'
                    };
    $http({
        method: 'JSONP',
        url: 'http://myapi.com/registrations.json',
        data: data,
        headers: headers
    }).
        success(function (data, status, headers, config) {
            console.log(data, status, headers, config);
        }).
        error(function (data, status, headers, config) {
            console.log(data, status, headers, config);
        });
}

我错过了什么?

您正在尝试传递dataheadersjsonp请求的参数是urlconfig。它不接受data(与其他类型的请求不同),并且headers从服务器返回,则无需将它们发送给服务器。我建议您使用 post 请求并返回 json 字符串(那是您使用标头时)。

我建议你阅读这个

除了确保服务器收到您的请求并正确发送响应之外。不确定您尝试将请求发送到何处,但http://myapi.com/registrations.json不存在。

我必须确保我允许服务器上的跨域。看似交叉多米安测试适用于邮递员,但当您实际尝试时则不然。