小编Jer*_*ter的帖子

CORS发布请求失败

我使用SLIM微型框架构建了一个API。我安装了一些使用以下代码添加CORS标头的中间件。

class Cors{

    public function __invoke(Request $request, Response $response, $next){

        $response = $next($request, $response);
        return $response
            ->withHeader('Access-Control-Allow-Origin', 'http://mysite')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }

}
Run Code Online (Sandbox Code Playgroud)

对于我的前端,我使用了VueJS。我设置了VueResource并使用以下代码创建了一个函数。

register (context, email, password) {
  Vue.http({
    url: 'api/auth/register',
    method: 'POST',
    data: {
    email: email,
    password: password
  }
}).then(response => {
  context.success = true
}, response => {
  context.response = response.data
  context.error = true
})
}
Run Code Online (Sandbox Code Playgroud)

在chrome中,以下错误会记录到控制台。

XMLHttpRequest cannot load http://mysite:9800/api/auth/register. Response to preflight request doesn't pass access …
Run Code Online (Sandbox Code Playgroud)

javascript slim cors vue.js vue-resource

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

V-如果与实现CSS下拉按钮冲突

我正在使用Materialize CSS来设计我的Web应用程序,并且我想根据用户是否通过身份验证来显示链接。我可以轻松地检查一下,但是当我在下拉按钮中添加v-if时,它将不再起作用。

这是我的代码:

<template>
  <ul id="account" class="dropdown-content" v-if="auth">
    <li><a href="#!" class="black-text"><i class="material-icons right">check_circle</i>Link 1</a></li>
    <li><a href="#!" class="black-text"><i class="material-icons right">folder</i>Link 2</a></li>
    <li><a href="#!" class="black-text"><i class="material-icons right">settings</i>Link 3</a></li>
  </ul>
  <nav class="white" role="navigation">
    <div class="nav-wrapper container black-text">
      <ul class="right hide-on-med-and-down">
        <li><a v-if="auth" class="dropdown-button" data-activates="account">{{ auth.first_name }} {{ auth.last_name }}<i class="material-icons right">arrow_drop_down</i></a></li>
        <li><a v-if="auth"><i class="material-icons right">exit_to_app</i>Logout</a></li>
        <div v-if="!auth">
          <li>
            <a v-link="{ name: 'registration' }">
              <i class="material-icons right">create</i>
              Sign Up
            </a>
          </li>
          <li>
            <a v-link="{ name: 'authentication' }">
              <i class="material-icons right">fingerprint</i>
              Login
            </a>
          </li>
        </div> …
Run Code Online (Sandbox Code Playgroud)

html javascript css materialize vue.js

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

如何在 Typescript 中分隔数组

我正在使用 Angular2 创建一个应用程序,并且我有很多参与方。

const PARTIES: Party[] = [
  { id: 1, title: "Main Event", description: "The biggest, most extravagant event in the last 10,000,000 years." },
  { id: 2, title: "Secondary Event", description: "The not as biggest, less extravagant event in the last 10,000,000 years." },
  { id: 3, title: "Another Event", description: "N/A" },
  { id: 4, title: "Another Event", description: "N/A" },
  { id: 5, title: "Another Event", description: "N/A" },
  { id: 6, title: "Another Event", description: …
Run Code Online (Sandbox Code Playgroud)

javascript arrays angularjs typescript

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

为什么recvfrom关心数据来自何处

所以,我在C中创建一个使用UDP的服务器,我想监听来自许多来源的传入数据包.因此,当我打电话时ssize_t recvfrom(int, void *, size_t, int, struct sockaddr * __restrict, socklen_t * __restrict),包含发件人信息的第五个参数可能会有所不同.

有没有办法在不知道每个客户的地址信息的情况下接收数据包?而且,这可能与C的库有关吗?

这是我的代码:

int file_descriptor;
char data[1024];
int bytes_recved;
sockaddr_in iDontKnow;
socklen_t addr_len = sizeof(iDontKnow);
if ((bytes_recved = recvfrom(file_descriptor, data, strlen(data), 0, (struct sockaddr*)&iDontKnow, &addr_len)) < 0) {
    perror("Failed to receive data");
}
Run Code Online (Sandbox Code Playgroud)

我注意到,当使用Java的DatagramSocket和DatagramPacket类接收数据时,DatagramSocket的receive函数接受了DatagramPacket 类型的参数.但是,此DatagramPacket仅保存放置数据的对象.那么,为什么C的UDP接收实现要求您知道发送者的信息?

c sockets udp network-programming recvfrom

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