CORS issues with jQuery Dropzone and upload to Imgur

后端 未结 2 468
悲哀的现实
悲哀的现实 2020-12-10 06:37

I tried to use jQuery Dropzone to upload an image to Imgur or any other domain but that\'s not working.

This is my dropzone setup:

$(\"div.dropzone\"         


        
2条回答
  •  青春惊慌失措
    2020-12-10 07:05

    You're running into the browser's same-origin security policy. Every browser has one; "origin" basically means "the same site". my JavaScript on example.com can access whatever it likes on example.com, but it's not allowed to read anything from demonstration.com, example.net, or api.example.com. They're from a different origin. Here's a table of what counts as the same origin.

    Without it, I could write a web page that steals all your gmail and private Facebook photos. My malicious JavaScript would make web requests to gmail.com and facebook.com, find the links to your emails & photos, load that data too, and then send it off to my own server.

    But some services, like APIs, are designed to be accessed by other services. That's where CORS comes in - Cross-Origin Resource Sharing. Web services can use CORS to tell browsers that it's fine to allow access from scripts. If you want to test your code by submitting to your own server, make sure your server is sending the required HTTP response headers.

    If you're developing locally, you must also be sure to test from a web server - an address beginning with http://, not file://. The protocol is part of the origin, so you can't submit to an http endpoint from a file URL.


    CORS has different types of requests. Some requests are considered simple requests, but others - requests with custom headers - require "preflighting". This means the browser will send a request to the server saying "Is this request OK for CORS?" using the HTTP OPTIONS method before sending the actual request. Any request with custom headers requires preflighting; that's where your HTTP OPTIONS is coming from. jQuery adds a custom X-Requested-With header to AJAX requests, so even if you hadn't added those you'd still see that Options request before the actual POST.

    From your screenshots, it looks like Imgur is going to allow your HTTP POST method. Let's move on to figuring out why that's not working.


    We're using the Imgur image upload API endpoint. This has one required parameter (image), and if we only want anonymous uploads all we need is a registered app. The upload method lets us send a simple URL to an image for upload, so let's try making an AJAX request to Imgur:

    $.ajax
      success: (data) -> console.log data
      type: "POST"
      data: 
        image: "http://placehold.it/300x500"
      url: "https://api.imgur.com/3/upload"
      headers:
        Authorization: "Client-ID *************" # Don't forget to put your actual Client-ID here!
    

    The next step is to try using the Filereader API to read the file from the form, and send that. Here's a CoffeeScript submit handler for that:

    $('#file-form').submit (ev) -> 
      ev.preventDefault()
      file = $('#file-form input[name=file]').get(0).files[0] 
      $.ajax
        success: (data) -> console.log data
        type: "POST"
        data: 
          image: file
        url: "https://api.imgur.com/3/upload"
        headers:
          Authorization: "Client-ID *************"
    

    Finally, we can try using Dropzone to achieve the same thing:

    $("div.dropzone").dropzone
      success: (file, response) -> 
        console.log file
        console.log response
      paramName: "image"
      method: "post"
      maxFilesize: 2
      url: "https://api.imgur.com/3/upload"
      headers:
        "Authorization": "Client-ID *************"
    

    The Dropzone success callback gets two arguments: the file that was uploaded, and the response from the server. You'll probably be most interested in the latter; Imgur sends back an id and a link parameter on success that you can use to show the user their newly-uploaded image.


    There's an example project for using the Imgur API from JavaScript available on Github here.

提交回复
热议问题