2017-02-15 112 views
0

我在Windows IIS(Internet信息服务)上托管HTML5,JavaScript和CSS3 https应用程序。如何根目录看起来是这样的:Javascript getJSON到IIS中使用jsonp变量的PHP文件

index.htmlsrc/index.jssrc/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(方法不允许)。谢谢。

+0

我想POST是被阻塞的IIS,因此405所以我应该坚持GET代替,这应该不被阻止。 – xinthose

回答

0

我会建议你去$就和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   } 
    }); 
+0

谢谢。我仍然404。 – xinthose

+0

嗨...我编辑我的答案...看看... –

+0

事实证明,我需要安装PHP的IIS,现在它的作品。 – xinthose