duanjiu4498 2016-08-31 14:58
浏览 57
已采纳

Ajax,json url只是特定的数据

I'm using this function

var url = 'XXXXXXXX'
        $.ajax({
            type: 'GET',
            dataType: "json",
          //  data: {Calling:Calling},
            processData: false,
            crossDomain: true,
            jsonp: false,
            url: url,
    success: displayAll
});
        function displayAll(r,data){
            for( i in r.Events )
                {           
                    var eventType    = r.Events[i].Event
                    var eventData    = r.Events[i].Data
                    var eventDetails = r.Events[i].Details
                    var caller = r.Events[i].Data.Calling

                    var test = JSON.stringify(caller);
                    console.log(test);
  }

The json XXX URL returns something like

 >Data: {
 >    Billing: "0033912312312" (OVH number)
 >    Body: "SIP/2.0 200 OK" (Header SIP packet)
 >    CallId: "N2ZkZmEyNGMxZWM0N2VhN2M4NGJkMTY4ZGYwZmU2OTY." (CallId identifier)
 >    Called: "0033912312312" (calling number)
 >    Calling: "0033912312312" (L'appelant)
 >    Cseq: "181 REGISTER" (Le code CSeq du paquet SIP)
 >    DateStart: Thu Mar 05 2015 12:44:30 GMT+0100 (CET) (La date)
 >    Dialed: "0033912312312" (Le numéro composé)
 >    Event: "registered" (Le type d'évenement)
 >    Protocol: "sip" (Le type de protocole, sip ou mgcp)
 >    RelevantInfo: "" (Information importante)
 >    Ts: 1425554670.102
 >    TsGet: 1425554670238926800 (Le timestamp de l'évenement en nanoseconde)
>}
>Date: "2015-03-05T12:24:30.255954 (CET)"
 >Details: {
 >    Description: "Gaelle Becquet" (Description du numéro OVH)
 >    Id: "295670" (Identifiant de la ligne du numéro OVH)
>    IdBillingAccount: "59415" (Identifiant du groupe du numéro OVH)
 >    SimultaneousLine: "2" (Nombre de lignes simultannées)
>    Type: "sipCirpack" (Type de ligne)
 >}
>Event: "registered" (Le type d'évenement)
>Ressource: "0033912312312" (Le numéro OVH)
>Service: "sip" (Le protocole)
>Timestamp: 1425554670255954700 (Le timestamp de l'évenement en nanoseconde)
>Token: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" (Le token)

I need to get the value of Calling and Called to re use it in sql query.

i've tried using

    data: {Calling:Calling, Called:Called},

in the ajax function but since im getting the result from url console returns

 ReferenceError: Calling is not defined

is there a way to pass it to php with data: ? or maybe an alternative (twig?)


EDIT : here is what i've tried

   var url = 'xx'

        $.ajax({
            type: 'GET',
            dataType: "json",
            //data: {Calling:Calling},
            processData: false,
            crossDomain: true,
            jsonp: false,
            url: url,
    success: displayAll


});

        function displayAll(r,data){
            for( i in r.Events )
                {
                    // Sotre in last events
                    var eventType    = r.Events[i].Event
                    var eventData    = r.Events[i].Data
                    var eventDetails = r.Events[i].Details
                    var caller = r.Events[i].Data.Calling

                    var test = JSON.stringify(caller);
                    console.log(test);
                    console.log(this);
                    var json = JSON.parse(test);
       $.ajax(
{
    url: 'test.php',
    type:'POST',  
    data: {caller:json},
    success: function(data)
    {
        console.log("ok");
        console.log(json);
    },
    error: function(data)
    {
      console.log("bug");
    }

});
 }

the console.log(json) works console returns :

ok
0033912312312

in the test.php file i've tried :

<?php
 $moui = $_POST['caller'];
 echo $moui;
 var_dump($moui);
 ?>

var dump return null

  • 写回答

1条回答 默认 最新

  • dqppv86022 2016-08-31 15:17
    关注

    You need to convert the string json to an object

    var json = JSON.parse(data);
    //Now access it like so
    json['Calling'];
    //or
    json.Calling;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号