简体   繁体   English

使用 javascript XMLHttpRequest 将数据(图像和一些其他参数)发送到 php

[英]Sending data (image and some other parameters) to php by using javascript XMLHttpRequest

This code is proper working i can send username and imgtype with url and images send by using sendAsBinary function and get this data in php but issue is here two parameter username and imgtype that i'm sending with url that get by using GET Method but i want to get in POST method because i don't want to show in url box. This code is proper working i can send username and imgtype with url and images send by using sendAsBinary function and get this data in php but issue is here two parameter username and imgtype that i'm sending with url that get by using GET Method but i想要进入 POST 方法,因为我不想在 url 框中显示。 I have try to send by using sendAsBinary function but not get in php just image get.我尝试使用 sendAsBinary function 发送,但没有进入 php 只是图像获取。 Please Help me i'm stuck on this from two days.请帮助我,我从两天开始就一直坚持这一点。 Thanks!谢谢!

         var xhr = new XMLHttpRequest();
            var params = "username="+username+"&imgtype="+imgtype;
            xhr.open('POST', upload_url+"?"+params, true);
            var boundary = 'someboundary';
            xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
            xhr.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                  alert(this.responseText);
                }
            };
            xhr.sendAsBinary(['--' + boundary, 'Content-Disposition: form-data; name="' + file_input_name + '"; filename="' + filename + '"', 'Content-Type: ' + type, '', data, '--' + boundary + '--','--' + boundary].join('\r\n'));

Using form data you can solve this problem Follow this link click Using form data you can solve this problem点击这个链接点击

A full working example is given below

HTML Code HTML 代码

<form enctype="multipart/form-data" method="post" name="fileinfo">
  <label>Username:</label>
  <input type="text"  name="username" placeholder="username" required/>
  <label>File to upload:</label>
  <input type="file" name="userfile" required />
  <input type="submit" value="submit form" />
</form>
<div></div>

Js code js代码

var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit', function(ev) {

  var outputDiv = document.querySelector("div");
  var dataSendToServer = new FormData(form);



  var requestObject = new XMLHttpRequest();
  requestObject.open("POST", "your_url", true);
  requestObject.onload = function(event) {
    if (requestObject.status == 200) {
      alert(requestObject.responseText)
    } else {
      alert("Error " + requestObject.status + " occurred when trying to upload your file")
    }
  };

  requestObject.send(dataSendToServer);
  ev.preventDefault();
}, false);

PHP code PHP代码

header("Access-Control-Allow-Origin: *");
        $responseData=array('imgType'=>$_FILES['userfile']['type'],'username'=>$_POST['username']);

        print_r(json_encode($responseData));

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

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