[英]How to send HTTP requests in phonegap?
I'm using phonegap to develop an inventory management mobile application and I can't figure out how to send http request without running into the CORS policy problem.我正在使用 phonegap 开发库存管理移动应用程序,但我不知道如何发送 http 请求而不遇到 CORS 策略问题。 I installed the cordova advanced http plugin but when I preview the app on chrome, the cordova object doesn't exist because the cordova.js script isn't added until later so I haven't really been able to tinker with the plugin.
I installed the cordova advanced http plugin but when I preview the app on chrome, the cordova object doesn't exist because the cordova.js script isn't added until later so I haven't really been able to tinker with the plugin. I simply want to send an http request and read back a JSON.
我只是想发送一个 http 请求并读回 JSON。 I have a URL.
我有一个 URL。 There is no username or password.
没有用户名或密码。 Please help!
请帮忙! I'm stuck!
我被困住了!
Update:更新:
I used this code:我使用了这段代码:
$.ajax({
url: 'https://api.barcodelookup.com/v2/products?barcode=075500000010&formatted=y&key=8f5uvzskkmjnptt43bz7yjzgwehscl',
data: null,
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
success: function() { alert("Success"); },
error: function() { alert('Failed!'); },
});
And the request went through, now, but the response is being blocked.现在请求通过了,但响应被阻止了。 I tried whitelisting by putting this in my xml:
我尝试通过将其放入我的 xml 来将其列入白名单:
allow-intent href="https://api.barcodelookup.com/*"
access origin="https://api.barcodelookup.com/*"
But I still get this error saying that the response was blocked:但我仍然收到此错误消息,表示响应被阻止:
jquery-1.11.1.min.js:4 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://api.barcodelookup.com/v2/products?barcode=075500000010&formatted=y&key=8f5uvzskkmjnptt43bz7yjzgwehscl&callback=jQuery111107483149196833481_1568855081600&_=1568855081601 with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
Well, you could simply add jQuery to your app and do好吧,您可以简单地将jQuery添加到您的应用程序中并执行
$.ajax({
url: "http://localhost/",
type: "POST",
crossDomain: true,
data: JSON.stringify(somejson),
dataType: "json",
success: function (response) {
var resp = JSON.parse(response)
alert(resp.status);
},
error: function (xhr, status) {
alert("error");
}
});
You could also try this depending on your backend (and post your data as a JSON string)您也可以根据您的后端尝试此操作(并将您的数据发布为 JSON 字符串)
$.ajax({
method:'POST',
url:url,
data:json_string,
async:true,
processData:false,
contentType:'application/x-www-form-urlencoded',
dataType:'json',
...
);
u can use this in your API php你可以在你的 API php 中使用它
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *'); header("Content-Type: application/json; charset=UTF-8");
header("Content-Type: application/json; charset=UTF-8");
Okay.好的。 I finally got it: I had to use the cordova advanced http plugin.
我终于明白了:我不得不使用 cordova 高级 http 插件。 I used the following code:
我使用了以下代码:
const options = {
method: 'get',
headers: { Authorization: 'OAuth2: token' }
};
cordova.plugin.http.sendRequest('https://api.upcitemdb.com/prod/trial/lookup?upc=075500000010', options, function(response) {
// prints 200
alert(response.status);
var obj = JSON.parse(response.data); // This is the resultant JSON in useful form
}, function(response) {
// prints 403
alert(response.status);
//prints Permission denied
alert(response.error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.