Answer the question
In order to leave comments, you need to log in
How to make JSON work in jQuery (Spring)?
Good day everyone :)
I'm trying to make an ajax JSON request in jQuery, the goal is to get a collection of elements, but for some reason it does not return anything after success:function(data), I tried all browsers and connected the json libraries, it did not help. If dataType: text, then the code of the page from which the request was sent comes.
I have already studied the entire Internet on this topic, but what they offer I already have everything, so I admit that maybe something is not installed or it is installed crookedly and because of this it does not work. Perhaps someone had a similar problem, if someone can tell me how to fix it, I will be grateful for the help :)
function getData(type) {
var pagelink = "getData/";
var url = pagelink +type+ ".html";
$.ajax({
url : url,
method : "get",
contentType:"application/json",
dataType : "json",
async: false,
success : function(data) {
alert("success");}});
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home( Model model) {
return "templeName";
}
@RequestMapping(value = "/getData/{type}", method = RequestMethod.GET)
public @ResponseBody List<String> showData(Model model, @PathVariable("type") String type) {
List<String> list= new ArrayList<String>();
list.add("2");
list.add("1");
return list;
Answer the question
In order to leave comments, you need to log in
Thank you all, I solved the issue, but not the way I wanted, but still. As it turned out, on the server side, the json request is not formed in the form in which it is needed, this must be done manually. Here is what happened on the server side
@RequestMapping(value = "/bean")
public class Controller {
@RequestMapping(value = "/getData", method = RequestMethod.GET)
public @ResponseBody String showData(Model model) throws JSONException{
JSONArray userArray = new JSONArray();
JSONObject userJSON = new JSONObject();
userJSON.put("id", "1");
userJSON.put("firstName", "Name");
userJSON.put("lastName", "LastName");
userJSON.put("email", "Email");
userArray.put(userJSON);
return userArray.toString();
}}
I copied your code almost one-to-one, but the problem could not be reproduced. Everything works as it should: specified text - received a string, specified json - received Array. Here is my code:
var url = "bean/getData";
$.ajax({
url: url,
method: "get",
contentType: "application/json",
dataType: "json",
async: false,
success: function (data) {
alert(data);
}
});
@RequestMapping(value = "/bean")
public class Controller {
@RequestMapping(value = "/getData", method = RequestMethod.GET)
public
@ResponseBody
List<String> showData() {
List<String> list = new ArrayList<String>();
list.add("2");
list.add("1");
return list;
}
}
and what exactly is sent back to the script?))) Since everything works with text, it means that the returned dataType does not correspond to the one declared in the script
Do you know that you are requested from JS to an address like /getData/type.html, and the server listens to addresses like /getData/type?
Try adding to the config:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question