以下Web服务呼叫的Web服务不起作用



我正在尝试通过一些jQuery调用Web服务,但是我会收到以下错误

err_spdy_protocol_error

当我尝试通过浏览器访问Web服务时,我会收回响应,

https://vik-b-it.000webhostapp.com/intergration.php?primaryref=1&Amount = 2& ampt = 3

{"指纹":" 1234 | Hello | 10 | 1 | 2 | 3"}

但是,当我通过以下代码调用它时,我会收到上述错误..我在这里缺少什么?

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;
	
	
	/* $results = 'This should be the return string'; */
	$results = $merchantId . '|' . $password . '|' . $txnType . '|' . $primaryRef .'|' . $amount .'|' . $fp;
	//$results = sha1($results);
	
	header('Content-type: application/json');
	echo json_encode(array('fingerPrint' =>$results));
	
}
?>

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){
  
	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 test@test.com');
            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 support@test.com');
            }
        });
    }  
});

,因为您传递给ajax请求的数据无效

您期望JSON,但您通过字符串

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

所以尝试将其更改为有效的JSON这里提示

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

相关内容

  • 没有找到相关文章

最新更新