jqm中可以使用HTTPRequest。不用设置权限,但是有限制,只能使用异步消息。
代码框架如下:
function submitPost(postString) {
var request = new XMLHttpRequest();
request.onreadystatechange=function() {
// Need to wait for the DONE state or you'll get errors
if(request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
console.log("Response = " + request.responseText);
// if response is JSON you can parse it
var response = JSON.parse(request.responseText);
// then do something with it here
}
else {
// This is very handy for finding out why your web service won't talk to you
console.log("Status: " + request.status + ", Status Text: " + request.statusText);
}
}
}
// Make sure whatever you post is URI encoded
var encodedString = encodeURIComponent(postString);
// This is for a POST request but GET etc. work fine too
request.open("POST", "https://<your_service_endpoint_here>", true); // only async supported
// You might not need an auth header, or might need to modify - check web service docs
request.setRequestHeader("Authorization", "Bearer " + yourAccessToken);
// Post types other than forms should work fine too but I've not tried
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Form data is web service dependent - check parameter format
var requestString = "text=" + encodedString;
request.send(requestString);
}
具体参见
本文详细介绍了如何在Cascades中利用XMLHttpRequest进行异步消息传递,实现与Web服务的交互。重点包括代码示例、设置及处理响应的方法。
1751

被折叠的 条评论
为什么被折叠?



