简体   繁体   English

即使是POST方法,Ajax也充当GET方法

[英]Ajax acting as GET method even though is POST method

Hello I have encountered a problem while coding in Javascript and PHP (Ajax non jquery). 您好,我在用Javascript和PHP(Ajax非jquery)编码时遇到问题。 I am trying to upload a file over Ajax, and handle it in PHP. 我试图通过Ajax上传文件,并在PHP中处理它。

This is my code: 这是我的代码:

index.html 的index.html

<html>
<head>
<title>PHP AJAX Upload</title>

<script type="text/javascript">


function upload() {
    // 1. Create XHR instance - Start
    var dat= "bla";
    document.getElementById("div2").innerHTML = "working";
   if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }

    var rad = document.getElementById('fajl');
    var filee = rad.files[0];

    var formData = new FormData();
    formData.append('rad',filee)
    formData.append('var',dat)

    xhr.open('POST', 'upload.php'); 


    xhr.send(formData);

    xhr.onload = function () {
        if (xhr.readyState === 4 && xhr.status == 200) {

                document.getElementById("div2").innerHTML = xhr.responseText;
                //alert(xhr.readyState);
                //alert(xhr.status);
             }       
    }
}

</script>
</head>
<body>

<form id="uploadForm"  enctype="multipart/form-data">
<label>Upload File:</label><br/>
<input name="rad" id="fajl" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" onclick="upload()" />
<div id="div2">
</div>

</form>
</body>
</html>

upload.php upload.php的

<?php

if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['rad']['tmp_name'])) {
$sourcePath = $_FILES['rad']['tmp_name'];
$targetPath = "images/".$_FILES['rad']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
echo ("uspjeh<br>");

}}
}
$podatak=$_POST['var'];
echo "$podatak"

?>

Problem is that I dont see PHP script response in my div2 element. 问题是我没有在div2元素中看到PHP脚本响应。 Ajax behaves wierd and it puzzles me. Ajax表现得很奇怪,这使我感到困惑。 I have put JavaScript alert command under xhr.readyState condition (now commented). 我已经将JavaScript Alert命令置于xhr.readyState条件下(现在已注释)。 When I do that then I see the output, but when I close alert dialog, the browser automaticly reloads page and makes the URL like i'm using GET method (i'm using POST) and then server output dissapears. 当我这样做时,我会看到输出,但是当我关闭警报对话框时,浏览器会自动重新加载页面,并使URL像我正在使用GET方法(我正在使用POST)一样,然后服务器输出消失。 (rad in ?rad=... is the name of my input element) (?rad = ...中的rad是我的输入元素的名称)

在此处输入图片说明

When I'm not using alert command then I don't see output at all, because page redirects really fast. 当我不使用警报命令时,我根本看不到输出,因为页面重定向的速度非常快。 What am I misiing? 我在想什么?

It's because you are using a submit button and that's submitting the form. 这是因为您使用的是“提交”按钮,并且正在提交表单。 By default form methods are GET requests. 默认情况下,表单方法是GET请求。 Change to just a button instead: 改为仅一个按钮:

<input type="button" value="Submit" class="btnSubmit" onclick="upload()" />

The default form action (submitting) is being carried out. 默认表单动作(提交)正在执行。

To stop this add return false to your click handler: 要停止此添加,请向您的点击处理程序return false

onclick="upload(); return false;" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM