douqu8828 2017-02-15 14:36
浏览 65

使用带变量的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.

  • 写回答

1条回答 默认 最新

  • dongqing7789 2017-02-15 14:43
    关注

    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            }
        }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码