2

下面的 api(s) 工作正常,我从“plan-api.php”得到正确的输出

PHP 代码(plan-api.php)

if ($_GET['result']):
$rslt = $_GET['result'];
$result = Unirest\Request::get("https://sphirelabs-mobile-number-portability-india-operator-v1.p.mashape.com/index.php?number=$rslt",
array(
"X-Mashape-Key" => "XXXXXXXXXX",
"Accept" => "application/json"
)
);

$optid= rawurlencode($result->body->Operator);
$cirid=rawurlencode($result->body->{'Telecom circle'});

endif;  


// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: XXXXXXXXX"               
 )
 );

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$requestUrl = "https://tariff-plan-api-datayuge.p.mashape.com/index.php?circleid=$cirid&limit=50&operatorid=$optid&recharge_type=3g";
$response = (file_get_contents($requestUrl, false, $context));

$data = json_decode($response, true);

$data2= json_encode($data);

echo $data2;

输出(来自 plan-api.php)

{"data":[{"id":"4401","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"60","recharge_talktime":"60","recharge_validity":"NA ","recharge_shortdesc":"Recharge of Rs 60 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4407","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"110","recharge_talktime":"110","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 110 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4409","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"150","recharge_talktime":"150","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 150 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4414","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"555","recharge_talktime":"600","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 555 By Reliance GSM","recharge_longdesc":"Extra Talktime","recharge_type":"Full Talktime"}]}

HTML/AJAX(plan-ajax.php)

在这里,如果我输入 的值,我将在文本框中得到输出,circleid但是operatorid当我尝试将第一个 api 的 php 对象输出时,我没有在下面的文本框中得到输出。$optid$cirid;

我尝试$('#opt'+key).val(value.operator);使用文本框 ID“opt”添加,但同样没有输出

Please enter a Mobile number
<input type="text" id="search">
<br>


<input type="text" id="result0">
<input type="text" id="result1">
<input type="text" id="result2">
<input type="text" id="result3">
<br>
<input type="text" id="talk0">
<input type="text" id="talk1"> 
<input type="text" id="talk2">
<input type="text" id="talk3">


<script>
$(document).ready(function() {
$('#search').keypress(function(){
    $.ajax({

        type: "GET",
        url: "plan-api.php",
        data: 'result=' + $('#search').val(),
        dataType: "json",

        success: function(responseText){

            $.each(responseText.data, function(key,value){

    $('#result'+key).val(value.recharge_amount);
       $('#talk'+key).val(value.recharge_talktime);

            });
        }

    }); // Ajax Call
}); //event handler

}); //document.ready
</script>
4

1 回答 1

2

API 的输出不正确,必须如下。基本上,它不是 JSON 格式。

{"data":[{"id":"4401","operatorid":"Reliance GSM","circleid":"Bihar   Jharkhand","recharge_amount":"60","recharge_talktime":"60","recharge_validity":"NA ","recharge_shortdesc":"Recharge of Rs 60 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4407","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"110","recharge_talktime":"110","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 110 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4409","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"150","recharge_talktime":"150","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 150 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4414","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"555","recharge_talktime":"600","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 555 By Reliance GSM","recharge_longdesc":"Extra Talktime","recharge_type":"Full Talktime"}]}

success:为了清楚起见,您可以在ajaxconsole.log(responseText)中添加。

从 中删除以下内容plan-api.php

echo $optid;
echo $cirid;
于 2015-07-19T05:30:18.143 回答