我们尝试在我们公司的 ERP 中实现基于长轮询的通知服务。类似于 Facebook 通知。
使用的技术:
timeout设置为 60 秒和 1 秒sleep。经过近一个月的编码,我们进入了生产阶段。部署几分钟后,我们不得不回滚一切。事实证明,我们的服务器(8 核)无法处理来自 20 名员工的长请求,每个员工使用约 5 个浏览器选项卡。例如:用户使用我们的 ERP 打开了 3 个选项卡,每个选项卡上都有一个长轮询 AJAX。打开第 4 个选项卡是不可能的 - 它会挂起,直到前 3 个选项卡中的一个被杀死(因此 AJAX 停止)。
“Apache 的局限性”,我们认为。所以我们去谷歌搜索。我找到了一些关于 Apache 的 MPM 模块和配置的信息,所以我试了一下。我们的服务器使用preforkMPM,apachectl -l如图所示。所以我在配置中更改了几行,看起来像这样:
<IfModule mpm_prefork_module>
StartServers 1
MinSpareServers 16
MaxSpareServers 32
ServerLimit 50%
MaxClients 150
MaxClients 50%
MaxRequestsPerChild 0
</IfModule>
Run Code Online (Sandbox Code Playgroud)
有趣的是,它可以在我的本地机器上使用类似的配置运行。在服务器上,看起来 Apache 忽略了配置,因为MinSpareServers设置为 16,它在重新启动后启动 8。当他不知道该怎么做。
在AngularJS中,我在子域中使用了Restful API但是我遇到的问题是cookie /会话没有跨域共享.对于Angular,我这样做:
app.config(['$httpProvider',
function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
Run Code Online (Sandbox Code Playgroud)
此外,当我用$ http发出请求时,我正在做
var object = {};
object.url = '/example'
object.withCredentials = true;
$http(object).success(object.success).error(object.error);
Run Code Online (Sandbox Code Playgroud)
在我的服务器端,我有:
if($_SERVER['REQUEST_METHOD']=='OPTIONS') {
if(isset($_SERVER['HTTP_X_FOWARDED_HOST']) && !empty($_SERVER['HTTP_X_FOWARDED_HOST'])) {
$origin=$_SERVER['HTTP_X_FOWARDED_HOST'];
} else {
$origin=$_SERVER['HTTP_ORIGIN'];
}
if(isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && ($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']=='POST' || $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']=='DELETE' || $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']=='PUT')) {
header('Access-Control-Allow-Origin: '.$origin);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: *,X-Requested-With,Content-Type');
//header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
// http://stackoverflow.com/a/7605119/578667
header('Access-Control-Max-Age: 86400');
}
}
Run Code Online (Sandbox Code Playgroud)
现在我看到服务器说它将允许凭据但不在选项请求中发送.截图如下.
我究竟做错了什么?
谁知道这有什么问题?我无法从我的wcf休息服务获得json响应.
jQuery的
$.ajax({
type: 'POST',
url: "http://localhost:8090/UserService/ValidateUser",
data: {username: 'newuser', password: 'pwd'},
contentType: "application/json; charset=utf-8",
success: function(msg) {
alert(msg);
},
error: function(xhr, ajaxOptions, thrownError) {
alert('error');
}
});
服务
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class UserService: IUserService
{
private readonly IUserRepository _repository;
public UserService()
{
_repository = new UserRepository();
}
public ServiceObject ValidateUser(string username, string password)
{
//implementation
}
}
[ServiceContract]
public interface IUserService
{
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
ServiceObject ValidateUser(string username, …Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个问题几个小时,虽然我在网络开发方面没有受过教育而无法理解.继承人:
另一个网站有一个脚本,他们从以下方式获取信息:
var url = "numbers.php";
parameters = "scoreid=" + document.getElementById('whatscore').value;
parameters += "&num=" + document.getElementById('num1b1').value;
xmlhttp2=GetXmlHttpObject();
if (xmlhttp2==null) {
alert ("Your browser does not support XMLHTTP!");
return;
}
xmlhttp2.onreadystatechange = function() {
if (xmlhttp2.readyState==4) {
scorespot.innerHTML=xmlhttp2.responseText; // load
setScores(document.getElementById('gradelvl').value); // set
document.getElementById('submitscorebtn').style.display="none";
}
}
xmlhttp2.open("POST",url,true);
xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp2.setRequestHeader("Content-length", parameters.length);
xmlhttp2.setRequestHeader("Connection", "close");
xmlhttp2.send(parameters);
Run Code Online (Sandbox Code Playgroud)
我试图做同样的事情,但是当我尝试它时,我得到了交叉原点错误.我知道他们有办法用jsonp和其他东西来做,虽然我不知道从哪里开始为此.
当我尝试直接从他们的页面请求信息时,可以访问numbers.php页面,例如example.com/numbers.php?scoreid=131&num=41.我总是返回"错误:参数语法不正确".
任何人都可以告诉我如何解决这个问题吗?我只熟悉PHP和Javascript,我对Ajax和其他东西或外部库都没有教育.
我感谢所有的帮助!注意:我无法访问WEBSERVER.
我正在尝试上传并通过AJAX提交表单,我发现了jQuery.form插件http://jquery.malsup.com/form/,这是我的代码:
$("#submitSlide").click(function(){
var options = {
success: function(data) {
console.log(data);
},
error : function( jqXHR , textStatus , errorThrown ){
console.log( ' broked ' , jqXHR , textStatus , errorThrown );
} ,
dataType: 'html',
type: 'POST',
url: 'http://www.slideshare.net/api/1/upload_slideshow'
};
$('#ssuploadform').ajaxSubmit(options);
return false;
});
Run Code Online (Sandbox Code Playgroud)
但是我收到这样的错误:
>>[jquery.form] Server abort: Error: Permission denied to access property 'document' (Error)
>>[jquery.form] cannot access response document: Error: Permission denied to access property 'document'
>>[jquery.form] aborting upload... aborted
Run Code Online (Sandbox Code Playgroud)
你知道如何解决这个问题吗?
谢谢,我感谢任何帮助!
如果我在这里问一个愚蠢的问题,请不要嘲笑我.
我已Cross Domain Issue多次听到,但实际上并没有真正参与其中.所以我在谷歌搜索它.但发现很多帖子都在谈论The cross domain issue when make a ajax call.甚至没有找到一篇文章来说明跨域问题究竟是什么,以及为什么不允许跨域?更多的问题是,如果我说跨域问题,是否意味着我向不同的域提出了错误的ajax请求?任何其他情况会导致此问题?谢谢.
我读的帖子是
首先,我知道网站上有类似的帖子,但没有一个能解决我的问题。
我想从 Web 服务器获取 JSON 文件。JSON 文件已由 JSONLint 检查,因此验证语法正确。JSON 数据是从类似于以下的 URL 检索的:www.examplewebsite.com/data.json
我的 jQuery Ajax 代码:
$.ajax({
url:url",
dataType:"jsonp",
contentType: "application/json",
type:"get",
success:function(){
console.log("OK");
},
error:function(data,status,error){
console.log(data " "status+" "+error);
}
});
Run Code Online (Sandbox Code Playgroud)
这是错误:
:parsererror 错误:未调用 jQuery222030055767520832166_1465026956736
到目前为止我所做的:
alert("OK");),但我仍然收到错误。错误消息之一是“未调用未定义”Access-Control-Allow-Origin错误更新1
我的json内容(用于测试):
[{
"id": 1,
"first_name": "Sara"
}, {
"id": 2,
"first_name": "Lois"
}, {
"id": 3,
"first_name": "Annie"
}, {
"id": 4,
"first_name": "Gregory"
}, { …Run Code Online (Sandbox Code Playgroud) 我的代码适用于同一个域.
但是当我在跨域使用 dataType:'jsonp'和crossDomain:true
代码示例时 -
var fa = new FormData();
fa.append("upload_pass", document.getElementById("upload_pass").files['0']);
$.ajax({
url: 'http://xxx.xx.xx.xx/upload.php',
data: fa,
contentType: false,
processData: false,
dataType: 'jsonp',
crossDomain: true,
type: 'GET',
success: function(data) {
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud)
是否有任何概念性理解差距或编码问题.
请建议.
我想使用从我的网站执行的javascript从其他网站获取数据.