jQuery: tanımsız sabit veri kullanımı verilerini 'üstlendi

2 Cevap php

Ben bir sunucuya senkron bir AJAX yazı yapmak ve geri JSON yanıt almak için jQuery kullanmak çalışıyorum.

Ben başarılı dönüşü üzerine bir javascript değişken msg ayarlamak istediğiniz

Bu benim kod gibi görünüyor:

$(document).ready(function(){
    $('#test').click(function(){
    alert('called!');
    jQuery.ajax({
      async: false,
      type: 'POST',
      url: 'http://www.example.com',
      data: 'id1=1&id2=2,&id3=3',
      dataType: 'json',
      success: function(data){ msg = data.msg; },
      error: function(xrq, status, et){alert('foobar\'d!');}
     });
});

[Edit]

Ben yanlışlıkla (şimdi düzeltilmiş) benim önceki xode PHP ve Javascript karıştırma edildi. Ancak, şimdi bu daha da şifreli hata iletisi alıyorum:

yakalanmamış istisna: [Exception ... "Bileşeni döndürülen hata kodu: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXMLHttpRequest.open]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" konum: "JS frame :: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js :: anonim: : satır 19 "data: no]

Ne ...?

2 Cevap

Lütfen success geri arama, Javascript ve PHP kodu karıştırma; Yapmak istediğiniz oldukça muhtemelen ne olduğunu.

Sen farkında olmalıdır:

  • PHP sunucu tarafında yürütülür; için. php dosyalarını
  • Javascript istemci tarafı (browser) üzerinde yürütülür


The function hooked on the success callback will only be executed on the client-side, where there is no PHP execution ; and it will receive the data sent by the PHP script on the server-side (The one receiving the Ajax request).

The data as received by the function hooked on success is a Javascript object.
Which means that, here, you might want to use something like this :

success: function(data){ msg = data.msg; },

Burada yani hiçbir PHP kodu.



Edit after the comment + edit of the OP

Bir "NS_ERROR_ILLEGAL_VALUE nsIXMLHttpRequest.open" hatası alıyorsanız; yani, ben size Ajax isteği yasadışı URL çeşit geçiyoruz ki, varsayalım.

Eğer jQuery.ajax için geçiyoruz URL geçerli biri olduğuna emin misin?

Örneğin:

  • Bu boş olmamalı
  • Bu uygulama aynı etki-adı var bir URL'ye işaret etmelidir

Senin çıktısını çalıştığınız dizge değildir. Deneyin:

success: function(data){ msg = <?php echo "data.msg;"; ?> },

Ben gerçekten sadece yapmak değil, neden noktasını görmüyorum:

success: function(data){ msg = data.msg },