简体   繁体   English

跨域上传图片

[英]Upload Images Cross Domain

I'm trying to make an upload images from one domain to another (CORS). 我正在尝试将图像从一个域上传到另一个域(CORS)。

The code works perfectly on Localhost but when I try in a real domain I keep receiving this message at the Developer Console: 该代码可以在Localhost上完美运行,但是当我在真实域中尝试时,我会在开发人员控制台上不断收到此消息:

Invalid request

upload-file.js: 上传-file.js:

function upload() {
  var formData = new FormData();
  formData.append('img', $('#profimg')[0].files[0]);
  $.ajax({
       url : 'https://example2.com/upload.php,
       type : 'POST',
       data : formData,
       cache: false,
       contentType: false,
       processData: false,
       crossDomain: true,
       success: function(h) {
         alert(h);
       }
});
}

upload.php: upload.php的:

<?php
    header('Access-Control-Allow-Origin: https://example1.com');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
    require 'c.upload.php';
    $tsUpload = new tsUpload();
    echo $tsUpload['response']; //It have to respond a message like Uploaded Image or Image cannot Upload, not the message that I receive on the developer console.

I tried everything, like changing headers, not sending data from the Ajax Function, changing from https to http, writing the code from-scratch, but I always get that message. 我尝试了所有操作,例如更改标头,不从Ajax函数发送数据,从https更改为http,从零开始编写代码,但我总是得到该消息。

My question is What is the problem? 我的问题是什么问题? Am I doing something wrong? 难道我做错了什么? Thank you. 谢谢。

If you got the Invalid request that means the request can not be create, that error is occur before http create .The $.ajax has a error() callback also, you can add it and check the detail error message. 如果您收到Invalid request ,这意味着该请求无法创建,则该错误发生在http create之前。 $.ajax也具有error()回调,您可以添加它并检查详细错误消息。

Buy the way, the page'url which you send the ajax is http or https 购买方式,您发送ajax的page'url是http或https

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

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