【问题标题】:jQuery Mobile HTTPSjQuery 移动 HTTPS
【发布时间】:2012-08-29 22:11:41
【问题描述】:

我正在使用 jquery mobile 和 phonegap 构建本机 Web 应用程序。数据从外部服务器加载。现在必须使用 ssl 加载数据,但出现错误:

NETWORK_ERR:XMLHttpRequest 异常 101

我尝试了 Chrome REST 客户端,一切正常。

代码:

WebService.prototype.execute = function (url, params) {
    var result = {
        HttpResponseObject : {},

        onSuccess : function (data) {       
            result.HttpResponseObject.response = JSON.parse(data);
        },

        onError : function (XMLHttpRequest, textStatus, errorThrown) {          
            $.mobile.hidePageLoadingMsg();          

            // TODO some logging for errors
            console.log(JSON.stringify(XMLHttpRequest));
            console.log(JSON.stringify(textStatus));
            console.log(JSON.stringify(errorThrown));       
        }
    };

    $.ajax({
        url:            url,
        type:           'POST',
        async:          false,
        data:           JSON.stringify(params),
        dataType:       'text',
        contentType:    'application/json',
        error:          result.onError,
        success:        result.onSuccess
    });

    return result;
};

【问题讨论】:

标签: jquery jquery-mobile ssl https


【解决方案1】:

这将对您有所帮助 - ajax 调用不适用于外部 url,但有一种解决方法:

How to call external url in jquery?

【讨论】:

  • Access-Control-Allow-Origin 不允许 Origin 127.0.0.1 仍然无法正常工作。给出同样的错误。
  • 您可能必须将 async 设置为 true,因为 async false 不适用于跨域请求。
【解决方案2】:

这是我为了使用 ajax 查询外部 API 所做的...

 $.ajax({
        type: "GET",
        url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[myAPIKey]&q=" + movie.title + "&page_limit=1",
        contentType: "application/json; charset=utf-8",
        crossDomain: true,
        dataType: 'jsonp',
        success: function (msg) {
            ...awesome code here
        }});

这里的关键是 "crossDomain: true" 行(对于某些服务也可能是 dataType: 'jsonp')。

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 2011-07-23
    • 2010-11-04
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多