从本地文件的jQuery阿贾克斯POST访问跨域不工作 [英] jQuery ajax POST from local file to access a cross domain not working

查看:243
本文介绍了从本地文件的jQuery阿贾克斯POST访问跨域不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想访问(POST)使用jQuery AJAX调用的Web URL,的http://主机:端口/ ... 的http://本地主机:8080 / ... 从本地的HTML文件, C:\ home.html的 。我无法得到它的工作。

我做了谷歌和这里看到了一些问题,但我不能得到它的工作。我需要一些帮助在这里。这是我到目前为止已经试过。

  1. 数据类型:JSONP
  2. 跨域:真正的
  3. 设置页眉在我的回应:

response.setHeader(访问控制 - 允许 - 起源,*);

去年3浏览器的工作 - 也就是说,FF或Chrome。请求永远不会到达服务器。下面是一些我看到的错误。

  1. 在无交通运输(IE)如果不是JSONP被使用。
  2. NS_BINDING_ABORTED /错误加载内容(NS_ERROR_DOCUMENT_NOT_CACHED)的FF

这是我的code。我想AP preciate任何帮助。我使用 jQuery的-1.8.2.min.js

  VAR HTTP_HOST =HTTP://本地主机:8080;

功能SU(PC,P){
    VAR suUrl = HTTP_HOST +/ps/api/v2/authorize.json;

    $阿贾克斯({
        键入:POST,
        网址:suUrl,
        数据: {
            phone_cell:PC,
            密码:对,
        },
        数据类型:JSON,
        跨域:真正的,
        成功:俄勒冈州立大学,
        错误:OE
    });
    返回false;
}

功能OSU(D){
    执行console.log(D);
}

功能OE(XHR,TS等){
    警报(SERVERERROR:+等);
}
 

的一个例子是一个完美的指针。

解决方案

我想我的code弄乱瓦特/所有不同的解决方案,我想。我终于能得到它的工作瓦特/设置标题(即建议,并努力为他人解决方案)。所有这一切我不得不这样做,以得到它的工作是以下内容添加到我的REST服务响应。

  response.setHeader(访问控制 - 允许 - 起源,*);
 

更新:

我想我想通了这一点,但我一点也不。还有更多的不只是设置了头。不管怎么说,在我的具体情况。我想特别是关于铬运行我的应用程序(HTML,JS)关闭硬盘驱动器,并试图访问可在云计算的Web服务。

下面是如何我终于解决了这个问题。我开始铬瓦特/下面的参数。

   - 禁用 - 网络安全--allow文件存取的档案
 

就像我前面提到的,这个程序是真的将要运行的铬嵌入式框架的一部分的桌面应用程序。

感谢每一位为您的输入。

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.

解决方案

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.

这篇关于从本地文件的jQuery阿贾克斯POST访问跨域不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆