GVKun编程网logo

使用php include或javascript ajax(php use include)

29

如果您对使用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)

使用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方法时出错

ajax php javascript:使用POST方法时出错

我在google上搜索过,关于stackoverflow的这个话题有很多问题.例如’数据不是通过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数据?

谢谢

解决方法

调用xhr.setRequestHeader(‘Content-type’,’application / x-www-form-urlencoded’);在你打电话给xhr.open之后

opts.data也应该是包含键/值对的字符串.例如键=值&安培;方法=交

AJAX返回的JavaScript不起作用.如何使用JavaScript?

AJAX返回的JavaScript不起作用.如何使用JavaScript?

这是我的页面Test1.asp
<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

默认情况下,不会执行包含AJAX响应的JavaScript.

当这个问题已经在各种库中解决时,就像jQuery和Prototype一样,从头开始构建Ajax处理程序毫无意义.

How to Auto Include a Javascript File_javascript技巧

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    try{ 
      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''] 



Our autoIncludeFunctions object should contain a list of JavaScript files, as well as a list of functions in those files. Here we are using shorthand JavaScript notation to create both the object and the arrays, but the same thing could be accomplished in many different ways. 

These .js files can contain any code you have available, such as JavaScript menus, animations, etc. The simplest example would be a file titled "file1.js" that only contained "function function1(){ alert(''Hello, World!''); }".

Note that if any of these files contain functions with the same name as another file, only the last function listed will be used. 

To make things a bit easier, we''re going to make a function that will pull a JavaScript file down and execute it. It''s very important, in our case, that the third paramater sent to the XMLHTTP object be false. This forces the script to wait for the response to download as opposed to continuing on with other code. 

复制代码 代码如下:

function loadScript(scriptpath, functions){ 
  var oXML = getXMLHttpObj(); 
  oXML.open(''GET'', scriptpath, false); 
  oXML.send(''''); 
  eval(oXML.responseText); 
  for(var i=0; i    window[functions[i]] = eval(functions[i]); 

The loadScript function expects two arguments: scriptpath and functions. "scriptpath" should contain the URL to your JavaScript file, and "functions" is the array of functions that exist in this JavaScript file.

As you can see, the code to pull and execute a script is straightforward. The browser first downloads, and then interprets the JavaScript file. If you''ve read any other articles on AJAX development, you might remember that in most cases the third argument sent to the open() function of an XMLHTTP object is usually "true." In our case we have it set to "false." This argument controls the state of the XMLHTTP object. If set to true, the object runs asynchrounously, meaning that all other JavaScript code continues while the object is loading. While this is a good thing in many circumstances, if we implemented it here our code would return before our file was done loading. Since we want our code to wait until this file is complete, we set this third argument to false, thus pausing our JavaScript execution until we are ready to continue. 

When the code is evaluated from the responseText, it''s executed in the limited scope of the loadScript function and because of this, none of these functions will be available outside of the loadScript function. In order do get around this, the for loop adds each function to the window object, thus making it globally available. 

It''s important to note that only scripts on the same server as the current page can be called in this manner. This is inherent to the XMLHTTP Object and is a necessary measure to increase general browser security. 

JavaScript .includes() 方法的多个条件

JavaScript .includes() 方法的多个条件

只是想知道,有没有办法向 .includes 方法添加多个条件,例如:

    var value = str.includes("hello", "hi", "howdy");

想象一下逗号状态“或”。

它现在询问字符串是否包含 hello、hi howdy。所以只有一个,并且只有一个条件为真。

有没有办法做到这一点?

答案1

小编典典

即使有一个,并且只有一个条件为真,那也应该有效:

var str = "bonjour le monde vive le javascript";var arr = [''bonjour'',''europe'', ''c++''];function contains(target, pattern){    var value = 0;    pattern.forEach(function(word){      value = value + target.includes(word);    });    return (value === 1)}console.log(contains(str, arr));

我们今天的关于使用php include或javascript ajaxphp use include的分享已经告一段落,感谢您的关注,如果您想了解更多关于ajax php javascript:使用POST方法时出错、AJAX返回的JavaScript不起作用.如何使用JavaScript?、How to Auto Include a Javascript File_javascript技巧、JavaScript .includes() 方法的多个条件的相关信息,请在本站查询。

本文标签:

上一篇如何跳过javascript数组中的javascript函数(js 跳过)

下一篇javascript – 如何在googlemaps网址中设置航点(google map怎么导航)