小编Nar*_*etz的帖子

AngularJS:POST数据到外部REST API

我有一个基本的AngularJS服务设置,如下所示:

app.factory('User', function($resource) {
return $resource('http://api.mysite.com/user/:action:id/:attr', {}, {
    history: {
        method: 'GET',
        params: {
            attr: 'history'
        }
    },
    update: {
        method: 'POST',
        params: {
            name: 'test'
        }
    }
});
});
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

User.history({id: 'testID'}, function(data) {
    console.log('got history');
    console.log(data);
});
User.update({id: 'me'}, function(data) {
    console.log('updated');
    console.log(data);
});
Run Code Online (Sandbox Code Playgroud)

问题一: User.update(),尽管将方法设置为POST,仍然继续发送OPTIONS作为请求方法.

虽然Chrome Dev工具报告了请求标头Access-Control-Request-Method:也会发送POST(不确定这是否意味着什么).

问题二:尽管在API代码中设置了这些标头,我仍然在使用CORS时出错:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
Run Code Online (Sandbox Code Playgroud)

如果发出非GET请求,则只会出现此问题.

处理这个问题的正确方法是什么?我也研究了JSONP,但是这是一个RESTful api,我不知道如何解决只有GET支持的问题.

javascript api cross-domain cors angularjs

16
推荐指数
2
解决办法
3万
查看次数

使用枚举作为类型允许枚举中不存在的值

我有这个打字稿代码(打字稿游乐场):

const enum Something {
  None = 0,
  Email = 10,
  All = 20
}

const enum Other{
  Email = 10;
  Value = 15;
}

interface Foo {
  prop: Something
}
const value2: Something = Something.None;

// Why can 15 be assigned if it's not in the enum?
const value: Something = 15;

// This errors:
const otherValue: Something = 'asdf';

const value3: Something = Something.NotExists;

const value4: Something = Other.Value;

const value5: Something = Other.Email;
Run Code Online (Sandbox Code Playgroud)

我不明白为什么在这种情况下 15 …

enums typescript

6
推荐指数
1
解决办法
684
查看次数

jquery / jquery.ui 的 google.load 是否已过时?

我刚刚与 Google js.api 中的 google.load 发生了奇怪的冲突,并加载了 jquery 和 jquery-ui。

当我使用 google.load 时

google.load('jquery', '1.7.1');
google.load('jqueryui', '1.8.17')
Run Code Online (Sandbox Code Playgroud)

这些版本是我能得到的最高版本,否则它会抛出“错误:模块:'jquery',找不到版本'1.8.1'!” 和类似的。

但是,它可以将文件包含在脚本标签中:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.js"></script>
Run Code Online (Sandbox Code Playgroud)

Google 托管库上,没有提到 google.load 作为替代方案,尽管我可以发誓前一段时间就有一个。但是,在Google Loader上,该示例中仍然包含 jquery 和 jquery-ui。谷歌忘记更新页面了吗?如果您不再需要使用 load 来加载库,那么此更改是什么时候引入的?

jquery jquery-ui

5
推荐指数
1
解决办法
2889
查看次数

如何根据传递的参数的值更改ui-router模板?

我设置了以下内容:

   var questionsContent = {
        name: 'questions.content',
        parent: 'questions',
        url: '/:question',
        views: {
            'content@': {
                templateUrl: '/Content/app/questions/partials/content.html',
                controller: 'QuestionsContentController',
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我真正想要的是:如果值为:问题是使用模板questionDetail.html的数字,如果不是数字则为content.html

有谁知道这样做的方法?

angularjs angular-ui-router

2
推荐指数
1
解决办法
2001
查看次数