使用带变量的jsonp将Javascript getJSON转换为IIS中的PHP文件
我在Windows IIS(Internet信息服务)上托管一个HTML5、JavaScript和CSS3 https应用程序。根目录的外观如下所示:使用带变量的jsonp将Javascript getJSON转换为IIS中的PHP文件,javascript,php,jquery,ajax,iis,Javascript,Php,Jquery,Ajax,Iis,我在Windows IIS(Internet信息服务)上托管一个HTML5、JavaScript和CSS3 https应用程序。根目录的外观如下所示: index.html,src/index.js,src/send\u payment.php 为了安全起见,我正在使用getJSON和jsonp从php文件返回一个简单字符串。以下是非工作代码: index.js var json_obj = { "data_value": 5489798123489725, "payment_a
index.html
,src/index.js
,src/send\u 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\u 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
。当使用相同的URL而使用AJAX POST时,我得到了错误405(不允许使用方法)。多谢各位 我建议您选择$.ajax并发布如下方法:
$.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 }
});
非常感谢。我仍然得到了404。事实证明,我需要为IIS安装php,现在它可以工作了。我认为帖子被IIS阻止了,因此405。所以我应该坚持使用GET,它不应该被阻止。