代码不向服务器发送请求

时间:2014-01-30 17:41:43

标签: javascript jquery cordova

我从我的应用发送请求时遇到问题。我在这里使用phonegap构建解决方案和其中一个插件 - 推送通知。代码如下:

 function tokenHandler(result) {
         var channelid;
         if (subParam == 2) {
            channelid = '2';
            } else if (subParam == 4) {
            channelid = '4,5';
            } else if (subParam == 1){
            channelid = '3';
            } else {
            channelid = '1';
            } 

            var url = "http://server.com/";
            var params = "device_token=" + result + "&device_type=ios&channels_id=" + channelid;
            http.open("POST", url, true);

            //Send the proper header information along with the request
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            http.setRequestHeader("Content-length", params.length);
            http.setRequestHeader("Connection", "close");
            http.send(params);

        alert("channel: " + channelid + "token: " + result);
    }

变量 subParam 位于标题中的单独<script>中。我认为请求有问题,但可能是错的。即使警报没有开火,但如果我移动到IF以上警报工作正常。感谢您的专业知识。

1 个答案:

答案 0 :(得分:1)

我认为您也可以将subParam作为参数传递给您的函数。喜欢:

function tokenHandler(result, subParam){

   if (subParam == '2') {
     // rest of code ...
   }
     // rest of code ...

根据CHAT更新

首先比较subParam =='2'(作为字符串),因为这是在别处定义的方式。

最终解决方案包括更改为将调用设置为AJAX:

ajax应该是:

$.ajax({ 
    url: url+params, 
    type: 'GET', 
    data: {'device_token': results, 'device_type': 'ios', ...[all parameters here] }, 
    success: function (result, status, XHR) { 
        alert(XHR.status); 
    }, 
    error: function () { 
        alert("error"); 
    } 
});

Chat Transcript