Ajax的方法显示输入错误错误、方法、Ajax

2023-09-10 21:04:27 作者:万千风景只愿与你分享

我张贴再一个问题,对此,我没有一个令人满意的答案:

I am posting again a question about which I didn't have a satisfactory answer :

我是新来的AJAX方法。我想发布一些相关信息是由一个PHP页面的进程,把它page.php

I am new to AJAX methods. I want to post some infos that are processes by a php page, call it page.php

在我的HTML页面,我已经把这个code:

In my html page, I have put this code :

<script type="text/javascript" charset="utf-8">
//I have put the function getXMLHttpRequest() on a separate js file
function getXMLHttpRequest() {
    var xhr = null;

    if (window.XMLHttpRequest || window.ActiveXObject) {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
        } else {
            xhr = new XMLHttpRequest(); 
        }
    } else {
        alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
        return null;
    }

    return xhr;
}

function request(callback) {
    var xhr = getXMLHttpRequest();

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
            callback(xhr.responseText);
        }
    };

    xhr.open("POST", "page.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("name=proposition");
}

function postData(Data) {

    alert(Data);

}
</script>

<button onclick="request(postData);">...</button>

在page.php,我有一个方法

In page.php, I have a method

function comment(){
  //some code processing the posted infos (that works fine)...
  //to debug, I have put a
  echo('Hello world');
}

更多precisely:page.php的结构 page.php页面='我的空间'和;后='后'。所以先有?的方向上一个mypage.php,然后在mypage.php有一类的instanciation。该 __构建方法将职位变量是评论()

More precisely : the structure of page.php is page.php?page='mypage'&post='post'.So first there is a direction to a mypage.php, and then in mypage.php there is an instanciation of a class. The __construct method takes the post variable to call a method within the class which is comment().

事实上,我没有得到任何​​世界,你好,而是一个巨大的警告消息与所有显示我的网页上code。 任何人有一个想法?

The fact is I don't get any 'Hello world' but a huge alert message with all my webpage code displayed. Anyone has an idea ?

最好的,Newben

Best, Newben

推荐答案

我同意MKK,也强烈建议您使用的jQuery 的AJAX的东西 - 它的安全,你的头痛的很多的稳妥地处理容易出错的流程为您服务。只需一条线在你的头段将其导入:

I agree with mkk and also strongly recommend using jQuery for Ajax-stuff - it will safe you a lot of headache and safely handle the error-prone processes for you. It takes only one line in your head-section to import it:

<head>
  ...
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  ...
<head>
<body>

<script>
  $('#my-btn').click(function() {

    var data = {'key1':'value1','key2':'value2','key3':'value3'};

    $.post('page.php', data, function(callback_data){
      alert(callback_data);
    });

  });
</script>

<button id="my-btn">Make an Ajax request!</button>

<body>

而在page.php:

And in page.php:

<?php
  echo 'The following data was sent: <br />';
  print_r($_POST);

通常你想获取从数据库的一些数据,然后把它写入你的前端。在这种情况下,它的共同使用JSON工作。 其实你应该看看一些有用的jQuery的功能,如后()和的连载()的,他们有很多的,这将非常给你的能力的想法很好的例子。如果你不喜欢jQuery的,这很好,你甚至不会注意到它的存在,而与正常的JavaScript工作。但是,当涉及到Ajax的请求,你真的应该使用jQuery。和谈论JSON,你可能还需要 json_en code ()在服务器端。

Usually you want to fetch some data from DB and then write it to your frontend. In this case it's common to work with JSON. Actually you should just have a look at some useful jQuery functions, like post() and serialize(), they have a lot of good examples which will quite give you an idea of the capabilities. If you don't like jQuery, it's fine, you won't even notice it's there while working with 'normal' JavaScript. But when it comes to Ajax-Requests, you really should use jQuery. And talking about JSON, you will probably also need json_encode() on the server side.

其实这是相当困难,指导您在一般情况下,请给多一点熟悉的技术和回来,如果你遇到一些问题,很难要求在谷歌。

Actually it's quite hard to guide you in general, please get a little more familiar with the techniques and come back if you encounter some problems that are hard to ask at google.

 
精彩推荐