Skip navigation.

Log in | Sign up

My little corner on MyOpera

Simple life :)

Parse single Javascript object in Jquery by $.ajax method

, , , ,

Chưa thử test với Jquery phiên bản mới nhất, mới chạy trên bản 1.2.1 (dự án hiện tại đang dùng :frown:). Khi sử dụng $.ajax như bên dưới thì phát sinh lỗi:
$.ajax({
type: "POST",
url: document.location.href,
data: "card_type=" + cardType + "&card_number=" + cardNumber,
dataType: 'JSON',
cache: false,
success: function(responses){
    if(!responses.has_error)
    {
 alert("Please enter valid Card Number.");
 return false;
    }
       }
});

thì bị lỗi. Mặc dù response là {'has_error': false;} nhưng lại không nhảy vào khối lệnh IF.
Cách khắc phục rất đơn giản:
...
success: function(responses){
    var obj = eval('('+responses+')');
    if(!obj .has_error)
    {
alert("Please enter valid Card Number.");
return false;
    }
}

Lưu ý: Chỉ xảy ra lỗi này nếu response là single object, nếu nó là mảng các object thì cứ $.each rồi get theo key, val thì lại ko sao :frown:

Xem bản có định dạng code tại đây

Beside youEntry for September 20, 2009

Comments

Dimibox 26. September 2009, 02:27

Chào anh.
Chúc anh có một ngày thứ 7 vui vẻ !

Và một đêm Chủ nhật ngon giấc !

namkhanh 26. September 2009, 11:51

cám ơn em & cũng chúc em như vậy :wink:

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

Download Opera, the fastest and most secure browser
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31