我试图通过一些Jquery调用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']))
{
/*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));
}
?>
&#13;
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>
&#13;
由于
如果有人对此感兴趣,最终的工作代码如下,
Webservice - &gt;
<?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代码 - &gt;
$(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');
}
});
}
});
答案 0 :(得分:0)
因为传递给ajax请求的数据无效
您期望JSON,但您传递字符串
var parameters = "{'primaryRef':'" + primaryRefNo + "','amount':'" + amount + "','fp':'" + fp + "'}";
所以尝试将其更改为有效的json 这里提示
var parameters = {primaryRef: primaryRefNo, amount: amount, fp: fp};