2017-02-18 88 views
0

我试图通过一些jQuery来调用Web服务,但我收到以下错误与Web服务调用下面的Web服务无法正常工作

ERR_SPDY_PROTOCOL_ERROR

当我尝试访问Web服务通过我的浏览器,我得到响应,

https://vik-b-it.000webhostapp.com/intergration.php?primaryRef=1&amount=2&fp=3

{ “指纹”: “1234 |你好| 10 | 1 | 2 | 3”}

但是,当我通过下面的代码调用它时,我得到了上面的错误..我在这里错过了什么?

Web服务代码

<?php 
 

 
/* require the user as the parameter */ 
 
if(isset($_GET['primaryRef']) && isset($_GET['amount']) && isset($_GET['fp'])) 
 
{ 
 

 
\t /*Set the variables */ 
 
\t 
 
\t $primaryRef \t = $_GET['primaryRef']; 
 
\t $amount \t \t = $_GET['amount']; 
 
\t $fp \t \t \t = $_GET['fp']; 
 
\t $merchantId \t = 1234; 
 
\t $password \t \t = 'Hello'; 
 
\t $txnType \t \t = 10; 
 
\t 
 
\t 
 
\t /* $results = 'This should be the return string'; */ 
 
\t $results = $merchantId . '|' . $password . '|' . $txnType . '|' . $primaryRef .'|' . $amount .'|' . $fp; 
 
\t //$results = sha1($results); 
 
\t 
 
\t header('Content-type: application/json'); 
 
\t echo json_encode(array('fingerPrint' =>$results)); 
 

 
\t 
 
} 
 

 
?>

的JavaScript代码是

<html> 
 
<head> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script> 
 

 
$(document).ready(function(){ 
 
    
 
    console.log('hello'); 
 
    
 
    GetFingerprint(1, 2, 3); 
 
    
 
    function GetFingerprint(primaryRefNo, amount, fp){ 
 
    
 
\t var divToBeWorkedOn = "#AjaxPlaceHolder"; 
 
    var webService = "https://vik-b-it.000webhostapp.com/intergration.php"; 
 
    var parameters = "{'primaryRef':'" + primaryRefNo + "','amount':'" + amount + "','fp':'" + fp + "'}"; 
 

 
    $.ajax({ 
 
     type: "GET", 
 
     url: webService, 
 
     data: parameters, 
 
     contentType: "application/json; charset=utf-8", 
 
     dataType: "json", 
 
     success: function(msg) { 
 
      $(divToBeWorkedOn).html(msg.d); 
 
     }, 
 
     error: function(e){ 
 
      $(divToBeWorkedOn).html("Unavailable"); 
 
     } 
 
    }); 
 
} 
 
    
 
}); 
 
</script> 
 
</head> 
 

 
<body> 
 

 
TEST PAGE 
 

 
<div id='AjaxPlaceHolder'> 
 

 
</div> 
 

 
</body> 
 

 

 
</html>

感谢

如果有人有兴趣的最终工作代码如下,

web服务 - >

<?php 
/* require the user as the parameter */ 
if(isset($_GET['primaryRef']) && isset($_GET['amount']) && isset($_GET['fp'])) 
{ 
    /*Set the variables */ 
    $primaryRef  = $_GET['primaryRef']; 
    $amount   = $_GET['amount']; 
    $fp    = $_GET['fp']; 
    $merchantId  = 1234; 
    $password  = 'Hello'; 
    $txnType  = 10; 

    //Concatanate the values together 
    $results = $merchantId . '|' . $password . '|' . $txnType . '|' . $primaryRef .'|' . $amount .'|' . $fp; 
    // Hash the information 
    $results = sha1($results); 
    // Encode data as JSON 
    $returnValue = 'jsonpCallback(' . json_encode(array('fingerPrint' =>$results)) . ');'; 

    // change the content type 
    header('Content-type: application/javascript'); 
    echo $returnValue; 
} 
?> 

,并呼吁jQuery代码 - >

$(document).ready(function(){ 

    //Generate the UTC date time 
    GenerateUTCDateTime(); 

    //Generate the fingerprint and pass the values from the three hidden fields - primary_ref, amount, fp_timestamp 
    GetFingerprint($('[name=primary_ref]').val(), $('[name=amount]').val(), $('[name=fp_timestamp]').val()); 

    // function to ensure that all dates are returned as two digits 
    // i = the number to checked 
    // returns two digit number 
    function addZero(i) 
    { 
     if (i < 10) 
     { 
      i = "0" + i; 
     } 
     return i; 
    } 

    // function that generates the current date and time as UTC 
    function GenerateUTCDateTime() 
    { 
     var dNow = new Date(); 
     var utc = new Date(dNow.getTime() + dNow.getTimezoneOffset() * 60000) 
     var utcdate= utc.getFullYear() + addZero((utc.getMonth()+1)) + addZero(utc.getDate()) + addZero(utc.getHours()) + addZero(utc.getMinutes()) + addZero(utc.getSeconds()); 
     //asign the date and time to the hidden field fp_timestamp 
     $('[name=fp_timestamp]').val(utcdate); 
    } 

    // function that populates the hiddent field with the fingerprint generated 
    function GetFingerprint(primaryRefNo, amount, fp) 
    { 
     // make sure that the three input paramters are available, otherwise throw an error 
     if (!primaryRefNo || !amount || !fp) 
     { 
      alert('Could not find the required values, please contact [email protected]'); 
      return; 
     } 

     // web service URL 
     var webService = "https://vik-b-it.000webhostapp.com/intergration.php"; 
     // input parameters that are passed 
     var parameters = JSON.parse('{"primaryRef":' + primaryRefNo + ', "amount":' + amount + ', "fp":' + fp +'}'); 

     // make an ajax call to get the data 
     $.ajax({ 
      type: "GET", 
      url: webService, 
      data: parameters, 
      contentType: "application/json; charset=utf-8", 
      dataType: 'jsonp', 
      jsonp: false, 
      jsonpCallback: "jsonpCallback", 
      success: function(data) {  
       $('[name=fingerprint]').val(data.fingerPrint); 
      }, 
      error: function(e){ 
       alert('Error has occured, please contact [email protected]'); 
      } 
     }); 
    } 
}); 
+0

尝试添加?在webService var的结尾?我没有在其他地方看到它 – Inkdot

+0

此外 - 你有没有看过“同源”政策?这可能与它有关。 – Inkdot

回答

0

因为你通过数据to ajax请求无效

您预期JSON,但您传递字符串

var parameters = "{'primaryRef':'" + primaryRefNo + "','amount':'" + amount + "','fp':'" + fp + "'}"; 

因此,尝试将其更改为有效的JSON 这里提示

var parameters = {primaryRef: primaryRefNo, amount: amount, fp: fp}; 
+0

感谢您的提示。就像你指出的那样,我必须传入一个Json对象。如果其他人感兴趣,我已经用解决方案代码更新了我的问题。 – user2206329