1

I am calling an AJAX function in my script from a server of IP - 128.119.30.12:7080 (for example)

Cross Domain: port number are different, so I get error of incompatibility.

            $.ajax({
                url: "http://128.119.30.12:9000/callback.php",
                data: dataObj,
                type: "POST",
                contentType: 'text/plain',
                sucess: function(data,status){
                    reply = data;
                }, 
                error : function(xhr, status, error){
                    reply = {"error": "Error in fetching data"}
                }
            }).done(function(reply) {
                replyCall(reply);
            });

            function replyCall(data) {
                console.log(data);
            }

I have enabled the mod_header function in my Apache server. And setting up header in the called php script - here is the script of php

<?php
    header('Access-Control-Allow-Origin: *'); 
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    print 'Hellooooooooo Response';
?>

It is still causing errors on chrome - XMLHttpRequest cannot load http://128.119.30.12:9000/callback.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://128.119.30.12:7080' is therefore not allowed access.

Niv
  • 11
  • 1
  • First fix your ajax you have a success and done callback? – Daan Jun 18 '15 at 12:38
  • Yes because I want to use the result to be submitted to the other function that called the ajax function. Okay, let's ignore done function. It still has the same problem. – Niv Jun 18 '15 at 13:00
  • 2
    Look in the Net tab of your browser's developer tools. Look for the request to callback.php. Does it look like you expect? Is it the method you expect? Do you get the response you expect? Does the response include any errors? Does it include the Access Control headers? – Quentin Jun 18 '15 at 13:05
  • Make sure you have [PHP error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) turned up as high as possible. – Quentin Jun 18 '15 at 13:06
  • Request Header `Host: 190.241.12.163:9204 User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 Accept: */* Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://190.241.12.163:8070/node-app/test.html Content-Length: 20 Origin: http://190.241.12.163:8070 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache` Here is the Response header- `Connection: keep-alive Content-Type: text/plain;charset=utf-8 Date: Fri, 19 Jun 2015 07:21:45 GMT Transfer-Encoding: chunked` – Niv Jun 19 '15 at 07:58
  • Another information port 9204 is running on NODEjs. I think that may be the main problem. We need to set header for nodeJS server instead of the Apache Server. – Niv Jun 19 '15 at 08:35
  • Guys, This problem is resolved. It was just to set header in the Node JS response instead of the PHP code. – Niv Jun 19 '15 at 10:12

0 Answers0