Javascript getJSON到IIS中使用jsonp变量的PHP文件
问题描述:
我在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方法象下面这样:
$.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 }
});
我想POST是被阻塞的IIS,因此405所以我应该坚持GET代替,这应该不被阻止。 – xinthose