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

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python