0

I am trying this JQuery code:

val = $(this).val();
var data = {
    "action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
    type: "POST",
    dataType: "json",
    url: "?getCustomer=1&sequence="+val+"",
    data: data,
    success: function(data) {
        alert(data["sequence"]);
    }
});

but the alert is returning undefined

if i check the URL (?getCustomer=1&sequence=4) i get this returned:

[{"sequence":"53"}]

so the sequence value is definately the

this is what shows in the console:

[Object]0: Objectsequence: "112"__proto__: Objectlength: 1__proto__: Array[0]concat: concat()constructor: Array()copyWithin: copyWithin()entries: entries()every: every()fill: fill()filter: filter()find: find()findIndex: findIndex()forEach: forEach()indexOf: indexOf()join: join()keys: keys()lastIndexOf: lastIndexOf()length: 0map: map()pop: pop()push: push()reduce: reduce()reduceRight: reduceRight()reverse: reverse()shift: shift()slice: slice()some: some()sort: sort()splice: splice()toLocaleString: toLocaleString()toString: toString()unshift: unshift()Symbol(Symbol.iterator): values()Symbol(Symbol.unscopables): Object__proto__: Object
7
  • What does console.log(data) show ? Commented Nov 20, 2015 at 16:13
  • check my update - wouldnt fit in comments Commented Nov 20, 2015 at 16:16
  • try data.sequence instead. json is an object, not an array. you can see that by the console.log results. Commented Nov 20, 2015 at 16:17
  • It returns an array, but you're accessing it like an object? Commented Nov 20, 2015 at 16:17
  • just tried data.sequence but still undefined Commented Nov 20, 2015 at 16:17

2 Answers 2

2

You misunderstood your json. Change

alert(data["sequence"]);

With

alert(data[0]["sequence"]);

or better

alert(data[0].sequence );
Sign up to request clarification or add additional context in comments.

Comments

0

[{"sequence":"53"}] is an array, the first element of which is an object with a sequence member. You need to alert(data[0]['sequence']).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.