Home Web Front-end JS Tutorial Solve the problem of spring mvc returning json data to ajax and reporting error parseerror

Solve the problem of spring mvc returning json data to ajax and reporting error parseerror

May 22, 2018 pm 03:28 PM
javascript json spring

Recently, when using ajax to receive json data from spring mvc, a parseerror error always occurs. Here, through this article, I will share with you the solution to the parseerror problem when spring mvc returns json data to ajax. Friends who need it can refer to it

Recently, when using ajax to receive json data from spring mvc, a parseerror error always occurs. The error source code is as follows:

Front end:

$.ajax({ 
      type: 'POST', 
      url: "groupFunctionEdit", 
      dataType: 'json', 
      contentType: "application/json", 
      data: JSON.stringify(functiondata), 
      success: function(data){ 
        alert('数据加载成功'+data.msg); 
      }, 
      error: function(xhr, type){ 
        alert('数据加载失败'); 
        console.log(type); 
      }
Copy after login

Backend Controller:

@RequestMapping("/groupFunctionEdit")   
  public @ResponseBody Object groupFunctionEdit(@RequestBody List<YyGroupFunction> yyGroupFunctionList) throws JsonProcessingException{ 
     
    return "success"; 
  }
Copy after login

Query the data and find the following answer:

When using simple types, that is : When using a type like String to receive data, there is no need to use the @RequestBody annotation.

Here you need to use spring mvc to process the dependent jar package of json: jackson.databind.jar

Solution:

No need to modify the front end, in the background Encapsulate the required data with map and convert it into String type:

@RequestMapping("/groupFunctionEdit")   
  public @ResponseBody Object groupFunctionEdit(@RequestBody List<YyGroupFunction> yyGroupFunctionList) throws JsonProcessingException{ 
     Map<String,Object> map = new HashMap<String,Object>(); 
     map.put("msg", "success"); 
     ObjectMapper mapper = new ObjectMapper(); 
     String msg = mapper.writeValueAsString(map); 
    return msg; 
  }
Copy after login

The data passed to the front end becomes:

{"msg":"success"}
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax cooperates with node js multer to implement the file upload function

##The principle of Ajax cross-domain request (Figure Text tutorial)

jQuery Validator verifies Ajax form submission method and Ajax parameter passing method (graphic tutorial)

The above is the detailed content of Solve the problem of spring mvc returning json data to ajax and reporting error parseerror. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Spring Boot and Spring AI to build generative artificial intelligence applications Use Spring Boot and Spring AI to build generative artificial intelligence applications Apr 28, 2024 am 11:46 AM

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese Mar 05, 2024 pm 02:48 PM

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

Application of JUnit unit testing framework in Spring projects Application of JUnit unit testing framework in Spring projects Apr 18, 2024 pm 04:54 PM

JUnit is a widely used Java unit testing framework in Spring projects and can be applied by following steps: Add JUnit dependency: org.junit.jupiterjunit-jupiter5.8.1test Write test cases: Use @ExtendWith(SpringExtension.class) to enable extension, use @Autowired inject beans, use @BeforeEach and @AfterEach to prepare and clean, and mark test methods with @Test.

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

How to use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values ​​of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

See all articles