Unexpected end of json input что это
Перейти к содержимому

Unexpected end of json input что это

JSON parsing error syntax error unexpected end of input

I am sure my data is in correct JSON syntax. When I checked with on Network of Chrome inspector the saveProduct request showed the data is correct.

This POST request did not have response. So I am clueless as to where the parse error was coming from. I tried using FireFox browser. the same thing happened.

Can anyone give some idea as to what is wrong?

P.S. Here is the controller code

user avatar

10 Answers 10

I can’t say for sure what the problem is. Could be some bad character, could be the spaces you have left at the beginning and at the end, no idea.

Anyway, you shouldn’t hardcode your JSON as strings as you have done. Instead the proper way to send JSON data to the server is to use a JSON serializer:

Now on the server also make sure that you have the proper view model expecting to receive this input:

and the corresponding action:

Now there’s one more thing. You have specified dataType: ‘json’ . This means that you expect that the server will return a JSON result. The controller action must return JSON. If your controller action returns a view this could explain the error you are getting. It’s when jQuery attempts to parse the response from the server:

This being said, in most cases, usually you don’t need to set the dataType property when making AJAX request to an ASP.NET MVC controller action. The reason for this is because when you return some specific ActionResult (such as a ViewResult or a JsonResult ), the framework will automatically set the correct Content-Type response HTTP header. jQuery will then use this header to parse the response and feed it as parameter to the success callback already parsed.

I suspect that the problem you are having here is that your server didn’t return valid JSON. It either returned some ViewResult or a PartialViewResult, or you tried to manually craft some broken JSON in your controller action (which obviously you should never be doing but using the JsonResult instead).

One more thing that I just noticed:

Please, avoid setting this attribute to false. If you set this attribute to false you are are freezing the client browser during the entire execution of the request. You could just make a normal request in this case. If you want to use AJAX, start thinking in terms of asynchronous events and callbacks.

Error: Unexpected end of JSON input when using empty package.json #12568

The problem you want to solve.
When I have an empty package.json file in nested folder, near checked files, eslint gives error:

There were similar issues previously — #11026 (comment), #7748 and there was no error in 5.16.0. Since 6.0.0 an error is thrown.

Your take on the correct solution to problem.
It would be nice to have an option to disable this error. I tried to ignore this json file, but didn’t succeed.

This is useful for me because I want this file to be invalid on purpose so later I can check it.

Are you willing to submit a pull request to implement this change?
Probably not, no free time to dive in atm.

The text was updated successfully, but these errors were encountered:

kaicataldo commented Nov 15, 2019

Could you also explain what you mean by «empty package.json «? Does the file literally have nothing in it or does it have <> ?

vladshcherbin commented Nov 15, 2019 •

so, in my situation I have a similar structure:

As you can see, here I have root package.json and .eslintrc.js config, both are valid and working. In tests/bad-json folder I have another package.json file, which is completely empty (not even a <> ).

When I try to lint tests folder, I believe eslint tries to read this nested tests/bad-json/package.json file to find eslint configuration and gives me an error.

I tried to add this package.json file to .eslintignore , override.excludedFiles , but didn’t have any luck. I got this error after updating from eslint v5 to v6.

image

I can also create a small reproduction repo if needed.

kaicataldo commented Nov 15, 2019

Thanks for the info! This will make it much easier to debug.

mysticatea commented Nov 15, 2019

When I try to lint tests folder, I believe eslint tries to read this nested tests/bad-json/package.json file to find eslint configuration and gives me an error.

You are right. In that case, you need /tests/bad-json in your .eslintignore , or a valid ESLint configuration file that has a higher priority than package.json such as /tests/bad-json/.eslintrc.yml .

Because ESLint reads /tests/bad-json/package.json when it entered /tests/bad-json directory in order to check if the config file change how ESLint traverses the subdirectory via RFC20 or RFC22. (so /tests/bad-json/package.json can have a setting that ignores/unignores /tests/bad-json/* .)

kaicataldo commented Nov 15, 2019

@mysticatea Do you think it would make sense to ignore empty package.json files, or at least only log a warning but not exit with an error code? Seems like if the package.json file doesn’t have any contents that it clearly is not intended to be used to configure ESLint.

mysticatea commented Nov 15, 2019

I feel odd if ESLint handles empty config files in special. A tests/bad-json/package.json may contain invalid JSON. So this is how ESLint handles parsing errors in config files.

The current strategy is to stop at a parsing error in config files. To print a warning and continue sounds good, but I have a concern about arbitrary outputs from outside of configured formatter.

mysticatea commented Nov 15, 2019

Honestly, I think reasonable if we going to deprecate config files in subdirectories.

vladshcherbin commented Nov 15, 2019

You are right. In that case, you need /tests/bad-json in your .eslintignore , or a valid ESLint configuration file that has a higher priority than package.json such as /tests/bad-json/.eslintrc.yml .

I tried different variants of adding /tests/bad-json to .eslintignore and only 1 variant worked — when I ignore the whole directory. However, I need files inside of this folder to be linted too, for example tests/bad-json/index.js . Some of my tries:

So, I believe current workaround in my situation is to have a higher priority config file in this folder.

platinumazure commented Nov 15, 2019

I think we should treat invalid or empty object package.json the same way as we treat lack of configuration files of any other type. The only thing in package.json that counts as an ESLint configuration is an eslintConfig key and object. Anything else should not matter to ESLint.

I think we should throw if the eslintConfig value is invalid for some reason (e.g., it’s an array or string rather than an object). But, if we can’t even see an eslintConfig key, then there is no configuration for ESLint to look at and we know this for a fact. Throwing on invalid JSON (for package.json) is unnecessarily intrusive, in my opinion.

mysticatea commented Nov 16, 2019

If parsing errors happened, it doesn’t know the errors are in eslintConfig or not. This is similar to that core rules cannot work if parsing errors happened.

Hmm, how about the following steps:

  1. Read the content of package.json
  2. Check if the content includes the constant string «eslintConfig» or not. If not, skip parsing.
  3. Parse the content as JSON.

Therefore, if the package.json file doesn’t contain ESLint config, ESLint skips the package.json file.

kaicataldo commented Nov 16, 2019

If parsing errors happened, it doesn’t know the errors are in eslintConfig or not.

This is a good point. As I think on this more, I’m of the mind that invalid JSON should still throw, but that if we wanted to ignore completely empty JSON files, that seems like reasonable behavior. As I think about this more, I actually think the new behavior in 6.0.0 makes a lot of sense.

@vladshcherbin Is there a particular reason the files can’t just contain <> to indicate they are empty? Keeping this behavior because some users might be relying on an error being thrown as a TODO/reminder feels out of scope of the tool’s area of responsibility.

vladshcherbin commented Nov 16, 2019 •

@kaicataldo in a plugin I have user can set input package.json file path, if this file has invalid json format (empty), I throw an error. In tests I want to test this case by providing invalid json file myself to see if correct error is thrown.

kaicataldo commented Nov 16, 2019

So, to clarify, you want this feature for testing purposes? If so, there are many assertion libraries that include built-in support for testing this, or you can use try/catch or Promises and assert in the catch block or in the rejection callback as well.

vladshcherbin commented Nov 17, 2019

@kaelzhang no, I use eslint only for linting, nothing else. When I try to lint tests folder which contains this empty package.json (created for tests) eslint throws an error when sees it and tries to use.

vladshcherbin commented Nov 17, 2019

In src folder function throws if passed package.json is invalid.
In tests folder there is test for this error.
In tests/bad-json there is this empty package.json file, used in this test.

When I try to lint src and tests folders, error is thrown. Hope this clears things up now 🙂

kaicataldo commented Nov 17, 2019 •

Appreciate all the info! I think the suggestion here is the right solution to this. As someone who works on language tooling, I’ve encountered these kinds of issues a lot, and what I tend to see done is to put test fixture files in a test/fixtures directory and ignore the whole directory when linting.

Uncaught SyntaxError: Unexpected end of JSON input

A common error encountered by JavaScript programmers is the Uncaught SyntaxError: Unexpected end of JSON input. This is usually observed when the coder is trying to convert a string into a JSON Object.

We will try to understand the cause of this error and how it can be solved. Let us look at an example.

Error Code:

Output:

We can observe here that the code raises the Uncaught SyntaxError: Unexpected end of JSON input.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *