2015-07-19 41 views
2

下面几个API,完美的工作,我正从 “计划 - api.php”阿贾克斯不是从API URL检测PHP字符串

PHP代码(计划 - 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; 
正确的输出

OUTPUT(从计划-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(计划-ajax.php)

这里我收到输出文本,如果我把牛逼他的值为circleidoperatorid但是当我试图把php对象输出的第一个API $optid $ cirid;比我在下面的文本框没有得到输出。

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

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> 
+0

的'从API $ response'的var_dump将是有益的 – Jigar

回答

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:你可以把console.log(responseText)的清晰度。

取下plan-api.php如下:

echo $optid; 
echo $cirid; 
+0

'信实%20GSMBihar%20Jharkhand'这是从第一API – user3588059

+0

输出@ user3588059好。然后删除'echo $ optid; echo $ cirid;'来自plan-api.php。我假设这是你的实际代码。 – Jigar

+0

ok..removed .....! – user3588059