P
P
Pirojochek2015-02-26 21:10:24
JavaScript
Pirojochek, 2015-02-26 21:10:24

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

5 answer(s)
P
Pirojochek, 2015-02-28
@Pirojochek

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();
      }}

V
Vladimir Smirnov, 2015-02-27
@bobzer

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;
    }
}

Try to copy as is to yourself and achieve performance. Use the browser debugger to view the Web and JS sources. If it works, add your specifics step by step, checking the performance after each change. It is likely that you made changes in several places at once, and everything broke at all because it was a switch to json. In my example, I simply change the dataType between text and json and immediately get a string or an array in data, respectively.

N
Nikita, 2015-02-26
@Panda_Tamara

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

K
Konstantin Kitmanov, 2015-02-26
@k12th

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?

N
Nick Smith, 2015-03-06
@Braidner

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 question

Ask a Question

731 491 924 answers to any question