2016-08-19 2 views
-3

Mein Script-Code wie:Benötigen In Script Hilfe

function changePrice(id) { 
    var url = '<?php echo $base_url ?>home/getprice/'; 
    $.ajax({ 
     url: url, 
     type: 'post', 
     data: 'id='+id, 
     success: function(msg) { 
      alert(msg); 
      /* 
      "regular_price": "800", 
      "discount_price": 720 
      */ 
     } 
    }); 
} 

Ich möchte sowohl regulären Preis und günstigen Preis auf separaten variable.How ??

+0

was in msg ist? console.log (msg), dann überprüfe die Konsole. –

+1

Funktionieren 'msg.regular_price' und' msg.discount_price' nicht für Sie? –

+0

ja, wenn msg ist ein Ziel, ich bin einverstanden mit @Swaraj Giri –

Antwort

0

Versuchen Sie folgendes:

In Server-Seite Ajax-Aufruf:

$respose['regular_price'] = 120; 
$respose['discount_price'] = 100; 
echo json_encode($response); 

In JS: msg Betrachtet man ein JSON-Objekt

var data = JSON.parse(msg); 
var regular = data.regular_price; 
var discount = data.discount_price; 
+0

Syntax: JSON.parse: unerwartetes Zeichen in Zeile 1 Spalte 1 der JSON Daten –

+0

success: function (msg) \t \t \t \t { \t \t \t \t \t var newdata = JSON.parse (msg); \t \t \t \t \t var normal = newdata.regular_price; \t \t \t \t \t var Rabatt = newdata.reduzierter Preis; \t \t \t \t \t Alarm (normal); \t \t \t \t \t \t \t \t \t} –

+0

ist die Rückführung Art von Nachricht ist kein json Objekt –

1

Wenn Sie die Antwort als "regular_price": "800", "discount_price": 720 dann werden immer machen es gültig JSON, analysieren Sie es und erhalten Sie die Eigenschaften.

var obj = JSON.parse('{' + msg + '}'); 
//  valid json -^-----------^- 

// get object properties 
var regular = data.regular_price; 
var discount = data.discount_price; 

UPDATE: Wenn Antwortdatenformat JSON gültig ist dann dataType: 'json' Option.

$.ajax({ 
    url: url, 
    type: 'post', 
    data: 'id='+id, 
    // set response datatype as json 
    dataType:'json', 
    success: function(msg) { 
     // get properties 
     var regular = msg.regular_price; 
     var discount = msg.discount_price; 
    } 
}); 

Oder es direkt analysieren, ob die Antwort eine Zeichenfolge ist.

$.ajax({ 
    url: url, 
    type: 'post', 
    data: 'id='+id, 
    success: function(msg) { 
     // parse the string 
     var data = JSON.parse(msg); 
     // get properties 
     var regular = data.regular_price; 
     var discount = data.discount_price; 
    } 
}); 
+0

o/p: \t { \t \t "REGULAR_PRICE": "50", \t \t "discount_price": 45 \t } success: function (msg) { \t var obj = JSON.parse ('{' msg + + '}'); \t var regular = data.regular_price; \t var rabatt = data.discount_price; \t Alarm (normal); } // SyntaxError: JSON.parse: erwartete Eigenschaft Name oder '}' in Zeile 1 Spalte 2 der JSON-Daten –

+0

@ZuberMafat überprüft aktualisierten Code –

0

Thanx zu all..Here sind die Lösung:

 <script> 
     function changePrice(id) 
     { 
      var url = '<?php echo $base_url ?>home/getprice/'; 
      $.ajax({ 
      url:url, 
      type:'post', 
      data:'id='+id, 
      dataType:'json', 
      success:function(msg) 
      { 
       var regular = msg.regular_price; 
       var discount = msg.discount_price; 
      } 
     }); 
    } 
    </script> 

Meine Funktion:

$new = array (
    "regular_price" => $result->price, 
     "discount_price" => $price 
    ); 
    $newarray = json_encode($new, JSON_PRETTY_PRINT); 
    print_r($newarray);