Home > Blockchain >  ajax returning json data as undefined
ajax returning json data as undefined

Time:12-09

when i run below code. it makes error and alert "fail error:StntaxError: Unexpected token < in JSON at position 0 data:undefined" what is the problem ??

$("#a").click(function () {
  st_dt = $("#st_dt").val();
  end_dt = $("#end_dt").val();
  lot_cd = $("#lot_cd").val();
  var obj = { st_dt: st_dt, end_dt: end_dt, lot_cd: lot_cd };
  var json_1 = JSON.stringify(obj);
  $.ajax({
    type: "POST",
    url: '{{ url_for("get_operid") }}',
    data: json_1,
    dataType: "JSON",
    success: function (data) {
      alert("Success\n"   data);
    },
    error: function (request, status, error, data) {
      alert("fail\n"   "error:"   error   "\n data:"   data);
    }
  });
});

CodePudding user response:

Looking at the code it looks like a Laravel API request using Blade Template or the Function url_for is in Flask... In either case

That means the response for the api request is HTML string instead of a json response...

i.e. The API request is returning a login page or some HTML page...

To check the response you can open the Chrome Devtools in the Network tab check the response of the API...

CodePudding user response:

what you can try is :

var obj = { st_dt: st_dt, end_dt: end_dt, lot_cd: lot_cd };
console.log(obj);
var json_1 = JSON.stringify(obj);
console.log(json_1);

Then See in browser console what is your object and if the JSON converting your object properly.

If that is ok , Your request should be done currectly. And try to see what are the data you getting as response with:

success: function (data) {
      consoel.log('response below');
      console.log(data);
 }

You will find the error. I hope.

  • Related