2012-04-20 51 views
0

我确定这是一个基本的语法错误,但我试图使用jQuery手机ajax(代码如下)进行休息调用,并且据我所知,ajax不会触发。试图触发jQuery Mobile ajax调用

 function triggerCall() { 
      alert("function triggered"); 
      $.ajax({ 
       type: "GET", 
       dataType: "jsonp", 
       url: REST Url, 
       success: function (data) { 
        alert(data); 
       } 
      }); 

任何帮助,将不胜感激

+0

任何错误或控制台消息? – 2012-04-20 22:50:10

回答

0

,如果你实际编写url: REST Url,那么您必须将其更改为一个变种,或者一个字符串..这将是一个问题

0

我相信,如果您使用JSONP,则需要在$ .ajax请求中指定回调,然后在REST文件返回中再次指定回调。下面是我一直使用的例子(尽管我不确定,但我确定)。

$.ajax({ 
    url: 'www.domain.com/string/to/your/REST/api', 
    data: { 
     dataToBeSent: variable, 
     dataToBeSent: sessionStorage.getItem('local/session Storage'), 
     dataToBeSend: "or a string" 
    }, 
    dataType: 'jsonp', 
    jsonp: 'jsoncallback', 
    timeout: 5000, 
    success: function(data){  
     alert("Huzzah!"); 
    }, 
    error: function(){ 
     alert("Boohisssss"); 
    } 
}); //end ajax call 

然后在URL,我会将此代码在文件的底部:

header("Content-type: application/json", true); 
echo $_GET['jsoncallback'] . '(' . json_encode($data) . ');'; 
exit; 

在哪里的数据是一个数组,它是JSON编码,在PHP中使用json_encode(),然后用回调函数($ _GET ['jsoncallback'])包装起来。

就像我说的那样,它并不完美,但它一直在为我工作。

0
  1. 检查浏览器控制台中的错误。例如。在铬菜单>工具> JavaScript控制台。
  2. 还建议将错误处理程序到你的Ajax调用:http://api.jquery.com/error/
  3. 确定哪种方法,JSONP或CORS,用于您宁静的Ajax调用:So, JSONP or CORS?