小编kam*_*808的帖子

如何使用HTML5 SSE发送json_encode数据

我有一个脚本,它触发一个SSE事件,用于从online.php获取json编码数据.在谷歌搜索,我找到了通过引入换行符来发送带有sse的JSON数据的方法.

我正在寻找的是当使用PHP的json_encode()函数创建JSON数组时如何通过SSE发送JSON.

我写了以下几行代码,但是有人可以帮助我在哪里添加SSE所需的"数据:\n \n"吗?

<script>
if(typeof(EventSource)!=="undefined")
{
  var source=new EventSource("online.php");
  source.onmessage=function(event)
  {
     var data=JSON.parse(event.data);
     $("#new_message").html("Inbox"+data['total']);
  };    
}
else
{
  $("#new_message").html("HTML5 not supported");
}
</script>
Run Code Online (Sandbox Code Playgroud)

online.php

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$data["total"]="hello";
echo json_encode($data);
ob_flush();
flush(); 
?>
Run Code Online (Sandbox Code Playgroud)

javascript php html5 json

6
推荐指数
2
解决办法
7365
查看次数

角度路由不适用于具有相同模块名称的控制器

这是一个简单的角度应用程序,似乎在代码中有一个愚蠢的错误,我不太能解决它.问题在于路由.单击链接不会将我带到指定的模板URL,而是重新加载相同的index.html页面.

但是,地址栏中的链接更改为:

HTTP://本地主机:8000 /应用/#/统计

HTTP://本地主机:8000 /应用/#/运动

点击相应的链接.

的index.html

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>My AngularJS App</title>
  <link rel="stylesheet" href="css/style.css"/>

  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="js/app.js"></script>
  <script src="js/controllers/myStats.js"></script>
  <script src="js/controllers/mySports.js"></script>

</head>
<body>
  <div class="container">
    <a ng-href="#/stats">My Stats</a>
    <a ng-href="#/sports">My Sports</a>
  </div>
  <div ng-view></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

app.js

'use strict';
angular.module('myApp', ['ngRoute'])
.config(['$routeProvider', function($routeProvider){
  $routeProvider
      .when('/stats', {
        templateUrl: 'partials/stats.html'


      })
      .when('/sports', {
        templateUrl: 'partials/sports.html'
      })
}]);
Run Code Online (Sandbox Code Playgroud)

我希望我的目录结构没有问题:

应用程序的目录结构

编辑:

共享控制器代码,问题出在控制器中.它必须用具有相同名称的角度模块做一些事情,尽管这就是我的教学方式.

JS /控制器/ mySports.js

angular.module('myApp',[])
    .controller('mySports',['$scope', function($scope){
        console.log('just checking');

    }]);
Run Code Online (Sandbox Code Playgroud)

什么工作: 从mySports.js更改模块名称 …

javascript angularjs angular-routing

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

为什么这个.not()选择器不起作用?

我有一个侧边栏响应式菜单,单击按钮时会打开.菜单有下拉子菜单,并使用twitter bootstrap模板.

当我点击菜单按钮和菜单本身以外的任何地方时,我试图隐藏菜单.

但点击菜单的例外不起作用.即使单击菜单,它也会隐藏.根据我的问题在于.not()选择器.

这可能有什么问题?

代码是:

CSS

.menu-click
{
    padding: 5px;
    cursor: pointer;
    border: solid 1px #ffcccc;
}

.mobile-menu
{
    top: 50px;
    display: none;
    position: absolute;
    z-index: 999;
    animation-duration:0.5s;
   -o-animation-duration:0.5s;
   -webkit-animation-duration:0.5s;
   -moz-animation-duration:0.5s;
}
Run Code Online (Sandbox Code Playgroud)

JQuery的

$(function(){

    $("html").not('.mobile-menu').click(function(){
        $(".mobile-menu").removeClass("animated fadeInLeft").addClass("animated fadeOutLeft")
    });
    $(".menu-click").click(function(event){
        event.stopPropagation();
    });
});
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

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

标签 统计

javascript ×3

angular-routing ×1

angularjs ×1

html ×1

html5 ×1

jquery ×1

json ×1

php ×1