我在Windows IIS(Internet信息服務)上託管HTML5,JavaScript和CSS3 https應用程序。如何根目錄看起來是這樣的:Javascript getJSON到IIS中使用jsonp變量的PHP文件
index.html
,src/index.js
,src/send_payment.php
我想在使用的getJSON與JSONP(安全)PHP文件的時刻返回一個簡單的字符串。這是不工作代碼:
index.js
var json_obj = {
"data_value": 5489798123489725,
"payment_amount": 10.50,
}
var json_str = JSON.stringify(json_obj);
$.getJSON("https://localhost:443/src/send_payment.php?callback=?", "json_str=" + json_str, function (data) {
try {
console.log("SUCCESS >> " + JSON.stringify(data));
}
catch (e) {
console.log("ERROR >> " + e.toString());
}
});
send_payment.php
<?php
try {
// 1. get data
$json_str = $_GET["json_str"];
// 2. parse the json string
$json_obj = json_decode($json_str);
// 3. get the parameters
$data_value = $json_obj->{"data_value"};
$payment_amount = $json_obj->{"payment_amount"};
}
catch (Exception $e) {
trigger_error("ERROR >> exception = " + $e->getMessage(), E_USER_ERROR);
}
return "test successful";
?>
我不知道如果代碼是正確的或丟失任何東西,但問題是我從getJSON獲得404(找不到頁面)。網址錯了嗎?我在本地訪問IIS,因此URL中的localhost
。當使用AJAX POST而不是相同的URL時,我得到錯誤405(方法不允許)。謝謝。
我想POST是被阻塞的IIS,因此405所以我應該堅持GET代替,這應該不被阻止。 – xinthose