2017-02-15 127 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