javascript - Why can jQuery request normally, but angularjs reports a cross-domain error?
高洛峰
高洛峰 2017-06-15 09:22:53
0
1
650

Report XMLHttpRequest cannot load http://example.com/v1/recharge. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.

   recharge.createRecharge = function (
          operatorId, operatorName, amount, extraAmount, userId, userName) {
          var deferred = $q.defer();
          var data = {
            operator: {
              user_id: operatorId,
              user_name: operatorName
            },
            amount: amount,
            recharge_amount: amount + extraAmount,
            currency: 'SGD',
            user: {
              user_id: userId,
              user_name: userName
            }
          };

          data = angular.toJson(data);
          if (data) {
            $http({
              method: 'POST',
              url: 'example.com/v1/recharge',
              headers: {
                'Content-Type': 'application/json'
              },
              data: data
            })
              .success(function (d, status) {
                  deferred.resolve(d, status);
                }
              ).error(function (err, status) {
              console.log(err);
            });
          }

          return deferred.promise;
        };
        

The following one works,

 var data = {
          'operator': {'user_id': '4320-9962-1b83f8fc4264', 'user_name': '乱石铺街'},
          'amount': 1,
          'recharge_amount': 2,
          'currency': 'SGD',
          'user': {'user_id': '4b92-ad75-506590099de5', 'user_name': 'ssdss'}
        };
        $.ajax({
          url: 'example.com/v1/recharge',
          method: 'POST',
          dataType: 'json',
          headers:{'Content-Type': 'application/json'},
          data: JSON.stringify(data),
          success: function (data) {
            alert(JSON.stringify(data));
          }
        });           
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
我想大声告诉你

Then you have to see what the value of your "REWARDS_SERVICE_HOST" is. The url in your ajax below is obviously a relative path, and the request is the interface address of the local project.

But if your "REWARDS_SERVICE_HOST" contains http://.../v1 and http://.../ is not completely consistent with the domain name port of your project, then it will be reported as cross-domain

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!