Home php error UNEXPECTED TOKEN & LT; IN JSON AT POSITION 0

error UNEXPECTED TOKEN & LT; IN JSON AT POSITION 0

Author

Date

Category

There is a code:

$ (document) .ready (function () {
$ ('Form'). Submit (FUNCTION (EVENT) {
  Var JSon;
  event.preventDefault ();
  $.ajax ({
    Type: $ (this) .attr ('Method'),
    URL: $ (this) .attr ('Action'),
    Data: New Formdata (This),
    CONTENTYPE: FALSE,
    Cache: False,
    ProcessData: False,
    SUCCESS: FUNCTION (RESULT) {
      JSON = jQuery.Parsejson (Result);
      if (json.url) {
        window.location.href = '/' + json.url;
      } else {
        alert (json.status + '-' + json.message);
      }
    },
  });
});
});
.......
& lt; Form Action = "/ admin / addbanner" Method = "POST" & gt;
& lt; div class = "Control-Group" & gt;
  & lt; div class = "Form-Group Floating-Label-Form-Group Controls" & gt;
    & lt; p & gt; & lt; Input Type = "text" class = "Form-Control" name = "name" placeholder = "name" & gt; & lt; / p & gt;
  & lt; / div & gt;
& lt; / div & gt;
& lt; div class = "Form-Group" & gt;
  & lt; Button type = "submit" class = "btn btn-secondary" id = "SendMessageButton" & gt; send & lt; / button & gt;
& lt; / div & gt;
& lt; / form & gt;

When you click on the button on the server in the $ _post variable there is nothing. I open the inspector code in chrome. There is such a mistake:
vm732: 1 uncaught Syntaxerror: UNEXPECTED TOKEN & LT; in json at position 0
AT FUNCTION.PARSE [AS PARSEJSON] ()
At Object.Success (Form.js: 13)
AT I (jquery.js: 2)
At Object.FireWith [As Resolvewith] (jQuery.js: 2)
AT A (jQuery.js: 4)
at xmlhttprequest. (jQuery.js: 4)

What is the problem here? Or tell me how to debug where to watch


Answer 1, Authority 100%

  1. Set the ContentType: 'Application \ JSon' >JS Script.
  2. Make sure your page on which the query is being queed sends data to JSON format. You can use the JSON_ENCODE () method on the server, if you have a PHP server. You can also send data in the form of xml , but for this it will be necessary to change the ContentType parameter: 'Application \ XML' .

In general, choose a certain rule for yourself, or an agreement, with which your front (JS + HTML, does not matter) will communicate with the server – here you will help headers that are just the same in the parameter . The most popular way to send \ receive data is JSON, it is supported on most platforms.


Answer 2

You return HTML instead of JSON.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions