【问题标题】:Not able to perform PUT and DELETE from angular js app无法从 Angular js 应用程序执行 PUT 和 DELETE
【发布时间】:2018-01-06 04:33:20
【问题描述】:

这是我的 .htaccess 文件。

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

# Cross domain access
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type, authorization"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Header add Access-Control-Request-Origin "*"
Header add Access-Control-Request-Headers "origin, x-requested-with, content-type, authorization"
Header add Access-Control-Request-Methods "PUT, GET, POST, DELETE, OPTIONS"

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

但是当我尝试执行 PUT 请求时,它给了我这样的错误。

:63342/web-wokoshop-partner/index.html?_ijt=vgfnsnd7c4g6l47qh3kt00i8c8#!/suppliers:1 XMLHttpRequest 无法加载 http://abcd.com/SupplierApi/v1/supplier/5。 预检响应包含无效的 HTTP 状态代码 404

这就是我向 angular 提出请求的方式

function updateSupplier(supplier, id) {
    var data = {
        name: supplier.name,
        mobile: supplier.mobile
    };

    var req = {
        method : 'PUT',
        url : REST_SERVICE_URI2 + '/' + supplier.id,
        data : JSON.stringify(data),
        headers : {
            'Content-Type' : 'application/json'
        }
    };


    $http(req).then(
        function(response) {
            alert('supplier updated.');
            console.log(data);
        },
        function(data) {
            consile.log(data);
            alert('update supplier error.');
            alert(data.statusText);

        });

}

有什么问题?我错过了什么吗?

【问题讨论】:

  • 尝试捕捉你的错误并通过添加.catch((error) => { console.log(error.response) })记录它

标签: javascript angularjs .htaccess cors


【解决方案1】:

试试这个删除:- $http.delete('Url/Delete', {params: {'Id': Id }}) 试试这个更新: - $http.put('Url/Update', {params: {'Id': Id }})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 2019-06-07
    • 2016-06-20
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多