2011-11-11 55 views
2

我有幾個關於使用這個問題。

1)我需要從谷歌代碼下載jquery.jsonp-2.1.4.min.js,但我應該放哪個文件夾? (我正在使用xampp)在C:\xamppC:\xampp\htdocs

2)我應該寫些什麼才能將一些字符串從網絡的$.jsonp傳遞到我的本地服務器?我應該在「數據」輸入中更改什麼來傳遞變量str

$.jsonp({ 
    "url": "http://localhost/server.php", 
    "data": { 
     "alt": "json-in-script" 
    }, 
    "success": function(userProfile) { 
     alert("ha"); // handle user profile here 
    }, 
    "error": function(d,msg) { 
     alert("Could not find user "); 
    } 
}); 

3)如何獲取php頁面中的變量「str」?

<?php echo str; ?> 

回答

0

當前的jQuery的版本有內置的JSONP支持。一個單獨的JSONP庫不再需要。

您可以使用它像這樣:

$.ajax({ 
    'url': "/path/to/your/endpoint.php?callback=?", 
    'dataType': 'jsonp', 
    'data': ... 
}); 

data的值作爲URL參數傳遞;因此,如果您使用PHP,它們將在$_GET中顯示。請注意,結果必須格式化爲JSON,幷包裝在回調中指定的函數中。適當的代碼可能會看起來像:

print $_GET['callback'] . '(' . json_encode($result) . ');'; 
+0

然而,我應該怎麼做,如果我使用$ .jsonp 似乎數據:{名稱=「A」}將包括URL參數的名稱.. ..這意味着我可以使用|| echo $ _GET ['name'] || ? – user1040948