使用带变量的jsonp在IIS中使用Javascript getJSON到PHP文件

I am hosting an HTML5, JavaScript, and CSS3 https application on Windows IIS (Internet Information Services). How the root directory looks is this:

index.html, src/index.js, src/send_payment.php

I am trying to return a simple string at the moment from the php file using getJSON with jsonp (for security). Here is non-working code:

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";
?>                  

I'm not sure if the code is right or missing anything, but the issue is that I get 404 (Page not found) from getJSON. Is the URL wrong? I am accessing IIS locally, thus the localhost in the URL. I get error 405 (Method not allowed) when using AJAX POST instead with the same URL. Thank you.

dongleman4760
dongleman4760 我认为POST被IIS阻止,因此405.所以我应该坚持使用GET,不应该被阻止。
3 年多之前 回复

1个回答



我建议您使用下面的$ .ajax和POST方法:</ p>

 < 代码> $ .ajax({
type:'POST',
url:'src / send_payment.php',
async:false,
data:{'data_value':5489798123489725,'payment_amount':10.5} ,
成功:功能(响应){
//做你想做的任何事情}
});

</ code> </ pre>
</ div>

展开原文

原文

I would recommend you to go for $.ajax and POST method like below:

  $.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            }
    }); 

douyi1966
douyi1966 事实证明我需要为IIS安装php,现在它可以正常工作。
3 年多之前 回复
dqfwcj0030
dqfwcj0030 嗨...我编辑了我的答案......看看......
3 年多之前 回复
dongxian7471
dongxian7471 谢谢。 我仍然得到404。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐