如果您对使用phpinclude或javascriptajax感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于使用phpinclude或javascriptajax的详细内容
如果您对使用php include或javascript ajax感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于使用php include或javascript ajax的详细内容,我们还将为您解答php use include的相关问题,并且为您提供关于ajax php javascript:使用POST方法时出错、AJAX返回的JavaScript不起作用.如何使用JavaScript?、How to Auto Include a Javascript File_javascript技巧、JavaScript .includes() 方法的多个条件的有价值信息。
本文目录一览:- 使用php include或javascript ajax(php use include)
- ajax php javascript:使用POST方法时出错
- AJAX返回的JavaScript不起作用.如何使用JavaScript?
- How to Auto Include a Javascript File_javascript技巧
- JavaScript .includes() 方法的多个条件
使用php include或javascript ajax(php use include)
我正在创建一个应用程序,我试图弄清楚最好的方法是我加载json文件.
我正在考虑的一种方法是PHP include
var jsonFile = <?PHP echo include "jsonFile.json";?>;
var jsonFile_2 = <?PHP echo include "jsonFile2.json";?>;
//and more possible includes
或Ajax更好?
$.ajax({
type: "GET",
url: "jsonFile.json",
dataType: "json",
success: function () {
//more nested ajax calls
}
});
或者两者的混合?
到目前为止,有3个json文件将被加载,其中一个文件会随着时间的推移而增长,每个用户都是唯一的,第二个是静态文件,第三个是大数据文件.所有用户都将访问第二个和第三个文件.
随着时间的推移,高用户增长,保持效率和良好性能的最佳方法是什么?当有1000,10000或1000000用户同时访问一个文件时,我应该担心PHP或javascript ajax的具体内容吗?是否应该采取不同的方法?
感谢所有的提示/建议和不同的方法.
解决方法:
这取决于你想要它的行为.每种方法都不同.如果你在$(document).ready上进行ajax调用,那么页面将首先加载,然后加载这两个文件.如果文件很大,那么你可能想要使用AJAX,因为这样一个调用就不会减慢整个页面的速度.如果你想要它们在页面加载,只需要它们一次,包含就好了.当您希望调用是异步时,AJAX是必需的.
ajax php javascript:使用POST方法时出错
但似乎不是我的问题
案件与其他问题几乎相同.这些是错误消息:
Firefox(第21版):
InvalidStateError: An attempt was made to use an object that is not,or is no longer,usable. xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
Chrome(第27版):
Uncaught Error: InvalidStateError: DOM Exception 11
当GET发送请求时,没有错误.并且所有GET数据都很好.
但是当由POST setRequestHeader发送时,itu会发生如上所述的错误.删除setRequestHeader后,错误消失了.没有错误,但没有收到POST数据.我是print_r($_ POST);那么数组是空的
问题已更新.这是来电者:
goServer({ url : 'users/sign-in.PHP',method : 'POST',data : 'randomId=' + randomId + '&name=' + name + '&password=' + password,done : function(response) { alert(response); },fail : function(response) { alert(response); } });
这是功能(对不起,长行):
function randomString() { var str = new Date().getTime(),str2 = Math.random(); str = str.toString(); str2 = str2.toString(); str2 = str2.substring(2,7); str += str2; return str; } function goServer(opts) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = requestComplete; function requestComplete() { if ( xhr.readyState === 4 ) { if ( xhr.status === 200 ) { opts.done(xhr.responseText); } else { opts.fail(xhr.responseText); } } } if ( !opts.method || opts.method === undefined ) { opts.method = "GET"; } if ( !opts.cache || opts.cache === undefined ) { opts.cache = false; } if ( opts.cache === false ) { opts.url += '?nocache=' + randomString(); } else { opts.url += '?nocache=false'; } if ( opts.method === "GET" ) { if ( opts.data !== '' && opts.data !== undefined ) { opts.url += '&' + opts.data; } opts.data = ''; } else { xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded'); } xhr.open(opts.method,opts.url,true); xhr.send(opts.data); }
注意,数据参数(opts.data)在GET发送时设置为url.当通过POST发送时,参数设置为xhr.send(opts.data);
问题:如何正确获取POST数据?
谢谢
解决方法
opts.data也应该是包含键/值对的字符串.例如键=值&安培;方法=交
AJAX返回的JavaScript不起作用.如何使用JavaScript?
<html> <head> <Meta http-equiv="Content-Language" content="en-us"> <Meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 1</title> <script type="text/javascript"> function Alex() { var xmlHttp; try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById("Alex").innerHTML =xmlHttp.responseText;//Get Google Destination Map } } xmlHttp.open("GET","Test2.asp",true); xmlHttp.send(null); } </script> </head> <body> <div id ="Alex"></div> <label onclick="Alex()" >ssss</label> </body> </html>
这是请求页面Test2.asp
<html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 1</title> </head> <body> <div id="Mathew"></div> </body> <script type="text/javascript" > { document.getElementById("Mathew").innerHTML='ajax is working'; } </script> </html>
在页面(Test2.asp)中javascript无法正常工作
如何使用ajax将test2.asp调用到我的test1.asp
当这个问题已经在各种库中解决时,就像jQuery和Prototype一样,从头开始构建Ajax处理程序毫无意义.
How to Auto Include a Javascript File_javascript技巧
Form: http://www.webreference.com/programming/javascript/mk/
Author:Mark Kahn
Many developers have a large library of JavaScript code at their fingertips that they developed, their collegues developed, or that they''ve pieced together from scripts all over the Internet. Have you ever thought that it would be nice to not have to search through all those files just to find that one function? This article will show you how dynamically include any JavaScript file, at runtime, by simply calling a function in that file!
Here''s an example: You have a function foo() in file bar.js. In your code, you know that foo() might be called, but it probably won''t be because most people do not use its functionality. You don''t want to force the user to download bar.js unless it''s going to be used because it''s a fairly large file. Here you''ll learn how to make a fake foo() function that actually loads bar.js on the fly and then calls the real foo() function.
Dynamically Loading a Script
As many developers know, there are at least two different ways to dynamically load a script at runtime. The first is to create a script object and append it to the document. The second is to use an XMLHTTP request to grab the source code, and then eval() it.
It is this second method that we''re going to use, and we''re going to exploit the fact that an XMLHTTP request has the capability to completely stall any script activity.
First, some basics: how to create an XMLHTTP Object. There are as many different functions to return a cross-browser XMLHTTP Object as there are developers that work with AJAX. I happen to have my own as well, and here''s a simplified example of that:
function getXMLHttpObj(){
if(typeof(XMLHttpRequest)!=''undefined'')
return new XMLHttpRequest();
var axO=[''Msxml2.XMLHTTP.6.0'', ''Msxml2.XMLHTTP.4.0'',
''Msxml2.XMLHTTP.3.0'', ''Msxml2.XMLHTTP'', ''Microsoft.XMLHTTP''], i;
for(i=0;i
return new ActiveXObject(axO[i]);
}catch(e){}
return null;
}
Most browsers other than Internet Explorer 5 or 6 have a built-in XMLHttpRequest object. Internet Explorer 7, when it''s released, will also have this object natively. The first thing we do is check to see if this object exists. If it does, we create an instance of it and that''s it. If the object doesn''t exist, we attempt to create one of several ActiveX Objects. We don''t know what objects our users have installed, so we attempt to create several different XMLHTTP objects, starting with the newest ones.
Now in order to dynamically load functions, we first need to define them. We could do this one function at a time, but instead of hard-coding dozens of functions, we can choose to just make an object or array with all the file names and the functions you want to have auto-included:
var autoIncludeFunctions = {
''scripts/file1.js'': [''function1'', ''function2'', ''function3''],
''scripts/file2.js'': [''function4'', ''function5'', ''function6''],
''scripts/file3.js'': [''function7'', ''function8'', ''function9'']
}