javascript - Ajax 未从 api url 检测 php 字符串

标签 javascript php jquery ajax mashape

下面的 api 工作正常,我从“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 $optid<code></code>$cirid; 的 php 对象输出时比我在下面的文本框中没有得到输出。

我尝试添加$('#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>

最佳答案

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"}]}

在 ajax 的 success: 中,为了清楚起见,您可以放置​​ console.log(responseText)

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

echo $optid;
echo $cirid;

关于javascript - Ajax 未从 api url 检测 php 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498239/

相关文章:

javascript - 连接 python 和 javascript 进行双向通信

php - MYSQL表结构

javascript - vanilla js 通过 XMLHttpRequest by div id 相当于 "Loading Page Fragments"

javascript - `element.next().insertBefore(element)` 是做什么的?

用于拆分数据和计算总和的 JavaScript

php - Paypal 沙盒 IPN : 14077410:ssl routines:ssl23_get_server_hello:sslv3 alert handshake failure

javascript - 在文本区域发送值

Javascript float 总和不起作用

javascript - 在查询字符串中发布嵌套对象 - Node.js

javascript - 动态显示按钮单击上的文本框