0

I have the following curl command which runs fine from bash shell

      $ curl  -d '{"img_url":"http://ec2-54-167-249-150.compute-1.amazonaws.com/IMG_86478_mono.bmp","engine":"tesseract"}' http://ec2-54-226-250-92.compute-1.amazonaws.com:8080/ocr

I now want to run this via a simple webpage with a button click. Here is my Javascript

      function processImage() 
      {   
          $.ajax({ 
                  url: 'http://ec2-54-226-250-92.compute-1.amazonaws.com:8080/ocr',
                  cache: false,
                  type : "POST",            
                  data: {   img_url : 'http://ec2-54-167-249-150.compute-1.amazonaws.com/IMG_86478_mono.bmp',
                    engine : 'tesseract' },
                  dataType: 'json',
                  success: function(response)
                  {
                      alert("success");
                  }    

            });
      }

but when I run this javascript via a webpage button click it gives me following error "NetworkError: 500 Internal Server Error - http://ec2-54-226-250-92.compute-1.amazonaws.com:8080/ocr" and my success() method is never called.

As I was saying I know there is nothing wrong with the server as it works fine when called from linux cmd prompt. Question is What is wrong with my javascript?

MayoMan
  • 4,757
  • 10
  • 53
  • 85

2 Answers2

0

Curl did work for me but when I try to access via jQuery I get an 500 error just like you. And when I google the error it almost always seems to be because of a missing argument in your .htaccess-file:

How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue?

CORS header 'Access-Control-Allow-Origin' missing

Community
  • 1
  • 1
talaub
  • 187
  • 6
0

You are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about how to achieve that is Using CORS.

Note - When you are using postman they are not restricted by this policy. Quoted from Cross-Origin XMLHttpRequest

Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy. Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.

Yogesh Koli
  • 95
  • 1
  • 7
  • Yes, this seems to be the problem. I guess I need access to the host server to add this CORS access file to it then? What should the ULR look like to get it to work when I paste it into a browser navigation window? Shouldn't that work same as running the request using curl? I have tried http://ec2-54-226-250-92.compute-1.amazonaws.com:8080/ocr?img_url=http://ec2-54-167-249-150.compute-1.amazonaws.com/IMG_86478_mono.bmp&engine=tesseract but it returns json format error :( – MayoMan Apr 25 '17 at 15:25
  • I guess you need to do this using server side script, for example using PHP script - have a basic api on your same server to call the url using curl(); does that make sense? – Yogesh Koli Apr 25 '17 at 15:40
  • sort of, if the XDomain issue can be worked around by having a proxy service on local server call the target url then what is the point in XDomain security? – MayoMan Apr 25 '17 at 15:46