简体   繁体   English

Ajax请求来自不同域的Restful API

[英]Ajax request to restful api from different domain

I have a restful api that works normally when i am calling from the postman. 我有一个宁静的api,当我从邮递员打电话时可以正常工作。 On postman i am giving the following configuration to get answer: -POST: " https://www.example.net/API/MyFunction " I have no authorization. 在邮递员上,我给出以下配置以获取答案:-POST:“ https://www.example.net/API/MyFunction ”我没有授权。 -Headers: Content-Type : application/json, username:myusername, password:mypassword -Body raw -Headers:内容类型:application / json,用户名:myusername,密码:mypassword -Body raw

{"firstparameter":"firtparameter_value"}

I have a pure html domain where i would like to make some calls and provide data from this api. 我有一个纯HTML域,我想在其中进行一些调用并从此api提供数据。 So i added a link to the jquery-3.3.1.js and create an ajax call. 因此,我向jquery-3.3.1.js添加了一个链接,并创建了一个ajax调用。 I tried to follow this guide https://www.html5rocks.com/en/tutorials/cors/ but when i run the call i am getting the bellow error. 我试图按照此指南https://www.html5rocks.com/zh-CN/tutorials/cors/进行操作,但是当我拨打电话时,出现了以下错误提示。 Ajax call: Ajax呼叫:

 var username = 'myusername';
 var password = 'mypassword';
$.ajax({

    type: 'POST',
    dataType: "jsonp",

    url: 'https://www.example.net/API/MyFunction',


    contentType: 'text/plain',
    crossDomain: true,
    data:{"firstparameter":"firtparameter_value"},
    xhrFields: {

        withCredentials: false
    },

    headers: {


        username: username,
        password: password
    },

    success: function () {


    error: function () {

    }
});

the error i am geting is : 我得到的错误是:

GET https://www.example.net/API/MyFunction/?callback=jQuery33104247946565223606_1541427224545&firstparameter=firtparameter_value&_=1541427224546 net::ERR_ABORTED 405 (Method Not Allowed) GET https://www.example.net/API/MyFunction/?callback=jQuery33104247946565223606_1541427224545&firstparameter=firtparameter_value&_=1541427224546 net :: ERR_ABORTED 405(不允许使用方法)

 type: 'POST', dataType: "jsonp", 

JSONP requests work by injecting a <script src> element which makes a GET request. JSONP请求通过注入发出GET请求的<script src>元素来工作。 The method property is ignored by jQuery when you use JSONP. 当您使用JSONP时,jQuery将忽略method属性。

You cannot make a POST request with JSONP. 无法使用JSONP发出POST请求。

A REST API is unlikely to return JSONP (which was a hack to work around the Same Origin Policy in the bad old days before we had CORS — and, like CORS, it must be set up on the server ). REST API不太可能返回JSONP(这是在我们拥有CORS之前的糟糕日子里解决Same Origin Policy的一种方法,就像CORS一样,必须在服务器上进行设置)。 Your API certainly does not because it is telling you the GET method (required for JSONP) is not allowed. 您的API当然不会,因为它告诉您GET方法(JSONP必需)是不允许的。

Set the correct value for dataType . dataType设置正确的值。


You have some other problems, but they are not related to the error message. 您还有其他问题,但它们与错误消息无关。

You said " -Headers: Content-Type : application/json " but then in the JS " contentType: 'text/plain' " — use the correct content-type. 您说过“ -Headers: Content-Type : application/json ”,但随后在JS中使用“ contentType: 'text/plain' “ —使用正确的内容类型。

You said " data:{"firstparameter":"firtparameter_value"} " but jQuery will encode that in URL format not JSON format. 您说的是“ data:{"firstparameter":"firtparameter_value"} ”,但是jQuery会以URL格式而不是JSON格式data:{"firstparameter":"firtparameter_value"}进行编码。 You need to explicitly encode it as JSON with JSON.stringify . 您需要使用JSON.stringify将其显式编码为JSON。

That error happens whenever you try to call an API function with the wrong Method, for example call a POST function with a Get Ajax call. 每当您尝试使用错误的方法调用API函数(例如,使用Get Ajax调用来调用POST函数)时,都会发生该错误。

You need to be sure that your backend has the method your calling from ajax as POST 您需要确保您的后端具有您从ajax调用为POST的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM