duanni5726 2019-08-19 08:43
浏览 434

我不能使用ajax从我的网站上使用Rest API,其中API和网站的端口不同

I have a Rest API developed in spring boot.It is running on port 9090.I have to consume the api from my website to get some data using angluarjs.The website is hosted in a shared hosting server which port is 80.Now there occurs problem in consuming the API from 80 port to a different port 9090.

I also tried using curl from php there is same issue occurs.

Here is my API : https://****url****:9090/aubwebsite-0.0.1-SNAPSHOT/api/document/alldocuments

AngularJs Controller is :

$scope.getDocuments = function(){
                var search  = {
                    pageNo : 1,
                    studentId:"",
                    studentName:"",
                    rowPerPage:11
                };
                $http({
                    method: "POST",
                    url: 'http://****url****:9090/aubwebsite-0.0.1-SNAPSHOT/api/document/alldocuments',
                    data: $httpParamSerializerJQLike(search),
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).then(function(response) {
                    console.log(response.data.documentList);
                    if(response.data.documentList === undefined || response.data.documentList.length == 0){
                        $scope.documents = [];
                    }else {
                        $scope.documents = response.data.documentList;
                        $scope.quantity = response.data.itemQuantity;
                    }
                    console.log($scope.documents,'All documents List');
                    console.log(response.data);
                });
            }

From the above call there is no api call take place.

How to consume an API from website whereas the port of the API and website deployed are different?

  • 写回答

1条回答 默认 最新

  • dongying7667 2019-08-19 08:50
    关注

    You need to implement Cross-Origin Resource Sharing, known as CORS.

    At its simplest, implement something like the following as a response to OPTIONS request:

    Access-Control-Allow-Origin: http://your-origin-website
    Access-Control-Allow-Methods: POST, OPTIONS
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Max-Age: 86400
    

    However, you claim you have the same issue when using curl from PHP. That shouldn't happen.

    Some further discussion on this site related to CORS:

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?