本文介绍了AngularJS发送像jQuery POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IM需要复制这个jQuery Ajax功能:

im need to replicate this Jquery ajax functionality:

 $.ajax({
    type: 'POST',
    url: 'https://api.url.gift/api',
    data: JSON.stringify({ "user_id": "100006" }),
    success: function(result) {
       console.log("asd");
       console.log(result);
    },

    error: function(xhr, ajaxOptions, thrownError) {
       console.log(xhr.status);
       console.log(thrownError);
    },
 });

这工作得很好,但是当我这样做是AngularJS:

Which works fine, but when I do this in AngularJS:

VAR的myData = JSON.stringify({user_ID的:100006});

var myData = JSON.stringify({"user_id": "100006" });

  $http({
     url: 'https://api.ecosquared.gift/things/cards/received_byme',
     method: 'POST',
     data: myData,
     headers: { 'Content-Type': 'application/json; charset=UTF-8'},
  }).success(function(data, status, headers, config){
     console.log(data);
  }).error(function(data, status, headers, config) {
     console.log(data);
  });

我得到:

XMLHttpRequest cannot load https://api.url.gift/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://url.gift' is therefore not allowed access. The response had HTTP status code 405.

一个奇怪的事情,如果我不发送数据参数,请求进入低谷和错误不要加薪,但当然不API返回我期望的数据。

One weird thing, if I dont send data parameter, the request goes trough and that error dont raises, but of course the API dont return the data I expect.

有人能指出我在正确的方向?

Can someone point me in the right direction?

推荐答案

尝试不将其转换为一个JSON字符串和没有报价发送数据:

Try sending data without converting it to a JSON string and without the quotations:

VAR的myData =({USER_ID:100006});

假设你user_id是一个整数

assuming you user_id is a integer

这篇关于AngularJS发送像jQuery POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:45