Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax POST from local file to access a cross domain not working

As the title says, I'm trying to access (POST) using jQuery AJAX call to a web url, http://host:port/... or http://localhost:8080/... from a local HTML file, c:\home.html. I can't get it to work.

I did Google and also saw several questions here but I can't get it to work. I need some help here. Here is what I've tried so far.

  1. dataType: jsonp
  2. crossDomain: true
  3. Setting the header in my response:
response.setHeader("Access-Control-Allow-Origin", "*");

None of the three browsers are working - IE, FF or Chrome. The request is never reaching the server. Here are some of the errors I'm seeing.

  1. No Transport (IE) if not jsonp is used.
  2. NS_BINDING_ABORTED / Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED) in FF

This is my code. I would appreciate any help. I'm using jquery-1.8.2.min.js.

var http_host =  "http://localhost:8080";

function su (pc, p) {
    var suUrl = http_host + "/ps/api/v2/authorize.json";

    $.ajax({
        type: 'POST',
        url: suUrl,
        data: {
            phone_cell: pc,
            password: p,
        },
        dataType: "json",
        crossDomain: true,
        success: osu,
        error: oe
    });
    return false;
}

function osu (d) {
    console.log(d);
}

function oe(xhr, ts, et)     {
    alert("ServerError: " + et);
}

An example would be a perfect pointer.

like image 385
user977505 Avatar asked Dec 11 '12 08:12

user977505


2 Answers

I suppose my code got messed up w/ all the different solutions that I was trying. I was finally able to get it to work w/ setting the header (solution that was recommended and worked for others). All that I had to do to get it to work is add the following to my REST service response.

response.setHeader("Access-Control-Allow-Origin", "*");

Update:

I thought I figured this out but I've not. There is more it than just setting the header. Anyways, in my specific situation. I was trying to run my app (html, js) off of the hard drive specifically on chrome and trying to access web services available on the cloud.

Here is how I finally solved the problem. I started the chrome w/ the following parameters.

--disable-web-security -–allow-file-access-from-files

Like I mentioned earlier, this app is really a desktop application that will be run as part of the chromium embedded framework.

Thanks every one for your input.

like image 77
user977505 Avatar answered Nov 15 '22 00:11

user977505


You can't make a cross-domain request from a local file because it's not on a domain. You need to host C:\home.html on a local webserver instance in order for it to work.

like image 31
Jivings Avatar answered Nov 14 '22 23:11

Jivings