skip to Main Content

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("Successn" + data);
    },
    error: function (request, status, error, data) {
      alert("failn" + "error:" + error + "n data:" + data);
    }
  });
});

2

Answers


  1. 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…

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search