[英]Javascript getJSON to PHP file in IIS using jsonp with variable
我在Windows IIS(Internet信息服務)上托管HTML5,JavaScript和CSS3 https應用程序。 根目錄是這樣的:
index.html
, src/index.js
, src/send_payment.php
我正在嘗試從php文件中使用getJSON和jsonp返回一個簡單的字符串(出於安全性)。 這是無效的代碼:
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(找不到頁面)。 URL錯誤嗎? 我正在本地訪問IIS,因此訪問URL中的localhost
。 當使用AJAX POST代替同一URL時出現錯誤405(不允許使用方法)。 謝謝。
我建議您使用$ .ajax和POST方法,如下所示:
$.ajax({
type: 'POST',
url: 'src/send_payment.php',
async: false,
data: {'data_value': 5489798123489725,'payment_amount': 10.5 },
success: function (response) {
//do whatever you want here }
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.