1

I have ajax like this:

$(".cq").on("input", function() {
  $.ajax({
    url: "create/reefFormAjax",
    type: "POST",
    data: $(this.form).serialize(),
    success: function(data) {
      callback(data);
    }
  });
});
function callback(response) {
  $(".sum_whl").val(response);
  $(".sum_rtl").val(response);
}

So in my controller I have something like this:

$test = $request->all();
return json_encode($test);

Then I retrieve data back to my view:

  {
    "material": "0",
    "mquantity": null,
    "cq": "2",
    "sum_rtl": null,
    "sum_whl": null
  }

The question is how I can get each value from response ?

I tried $(response).find('cq').val() or filter but I an have error.

Any ideas?

0

4 Answers 4

1

The result you get is a JSON response

  var response = 
  {
    "material": "0",
    "mquantity": null,
    "cq": "2",
    "sum_rtl": null,
    "sum_whl": null
  };

If the JSON is in your response variable, you can simply access the element like this

response.material; // should print 0 response.cq; // will print 2

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, i have response but when i'm trying console.log(response.cq); it's tell me that value = undefined...
0

When you return a json(or array) from laravel it automatically turn into a js object. so you can access like this:

function callback(response) {
  $(".sum_whl").val(response.sum_whl);
  $(".sum_rtl").val(response.sum_rtl);
}

4 Comments

It still return nothing... I tried like you saying before
put console.log(arguments); inside callback() That will show what exactly is received by the function.
Arguments ["{"material":"0","mq":"2","cq":"3","sum_rtl":null,"sum_whl":null}", callee: ƒ, Symbol(Symbol.iterator): ƒ] it have) but not showing) when using response.cq or response.mq using console.log() - undefined
@Moramu looks like you don't return an array. try these two: 1.instead of using this: response.sum_whl try this: response[0].sum_whl 2.if it is't working try` console.log(data);` and tell me what you get
0

console.log(response.data[0]) or console.log(reponse.material) must be work. Because its json object.

Give it a try.

$("#div).html(response.data[0])

$("#div).html(response.material)

Comments

0

I solved this problem, i was using return json_encode instead of return response->json($var); So i grab my values!

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.