使用Ajax请求的问题(科尔多瓦) [英] Issues with ajax request (Cordova)

查看:193
本文介绍了使用Ajax请求的问题(科尔多瓦)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的科尔多瓦应用程序发出的请求的一些问题。 无论Ajax请求的工作,我也可以包括这样的图片:< IMG SRC =htt​​p://www.bing.com/s/a/hpc14.png>

i have some issues with outgoing requests in my cordova app. neither ajax request's are working, nor i can include images like this: <img src="http://www.bing.com/s/a/hpc14.png">.

config.xml文件:

config.xml:

<plugin name="cordova-plugin-whitelist" version="1" />

<access origin="*" />

<allow-navigation href="*" />
<allow-intent href="*" />

<content src="index.html" />

app.js:

app.js:

$.ajax({
    url: "http://domain.xyz",
    type: "post",
    data: { "uuid": device.uuid },
    dataType: "json",
    success: function(json){
        alert('it works');
    },
    error: function(e){
        alert("status: "+e.status);
    }
});

这将返回送花儿给人状态:0

你能帮助我吗?

推荐答案

如果您使用的是科尔多瓦5,设备和服务器都可以看到对方,你很可能要启用此与内容安全策略。请参见科尔多瓦白名单插件。你要考虑增加一个元标记

If you are using Cordova 5 and the device and server can see each other you likely have to enable this with the content security policy. See Cordova Whitelist Plugin. You want to consider adding a meta tag to

配置示例如下所示:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

根据什么您的应用程序或不这样做,你可能不需要在上面的所有其他选项,虽然一些Ajax和模板框架将。对于科尔多瓦5应用程序中使用jQuery和把手上述作品。

Depending on what else your app does or doesn't do you may not need all other options in the above, although some Ajax and templating frameworks will. The above works for a Cordova 5 app using JQuery and Handlebars.

此外,对于X code 7/9的iOS则需要调整ATS设置为允许连接到非HTTPS后端:

Additionally for Xcode 7 / iOS 9 you will need to adjust the ATS settings to allow connections to non https backends:

下面是改变你的应用程序的信息的.plist的工作示例:

Here's a working example of the change to your app's info .plist:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

这就是你可以用一个pre构建挂钩的iOS做到这一点的脚本自动:

And here's a script you could use as a pre build hook for iOS to do this automatically:

#!/bin/bash
echo "Adjusting plist for App Transport Security exception." val=$(/usr/libexec/plistbuddy -c "add NSAppTransportSecurity:NSAllowsArbitraryLoads bool true" platforms/ios/PROJECTNAME/PROJECTNAME-Info.plist 2>/dev/null) echo "Done"

只是换了 PROJECTNAME 为您的项目的名称。

Just swap out PROJECTNAME for the name of your project.

这篇关于使用Ajax请求的问题(科尔多瓦)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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