简体   繁体   English

在ajax调用中遇到解析错误,但数据已成功传递

[英]Getting a parse-error in ajax call but the data is getting passed successfully

So I have basic form that takes an address, gets the latitude/longitude then im trying to pass the lat/long value to my API. 因此,我有一个基本形式,该形式需要一个地址,获取经度/纬度,然后我试图将经度/纬度值传递给我的API。 The API is getting the variables but im getting a parsing error in my ajax call after its successfully passed. 该API正在获取变量,但是在成功传递后,我的ajax调用中出现了解析错误。

<form method="GET">
     <input id="address" type="text">
     <button id='searchRadius' type="submit">Search</button>
</form>  

ajax call 阿贾克斯电话

$('#searchRadius').on('click', function (e) {

    var lati;
    var lng;
    var rad = 30;
    var address = $('#address').val();


    e.preventDefault(); 


    $.ajax({
        type: "GET",
        url: "http://www.mapquestapi.com/geocoding/v1/address?key=LIhb6pFxB7qAlFC4Aiul9rM9i7R5BcgB&location=" + address,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-type", "application/json");
        },
        success: function postForm(response) {
            lati = response.results[0].locations[0].latLng.lat;
            lng = response.results[0].locations[0].latLng.lng;

            console.log(lati, lng, rad);

            $.ajax({
                url: contextRoot + "/map/radius",
                type: "GET",
                data: {"lati": lati},
                dataType: "json",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Accept", "application/json");
                    xhr.setRequestHeader("Content-type", "application/json");
                },
                success: function (data, status) {
                    console.log(data);

                },
                error: function (data, status) {
                    alert("bad api call");
                    console.log(status);
                }
            });
        },
        error: function (data, status) {
            console.log(data.errors);
        }}
    );
});  

api call api调用

 @RequestMapping(value = "/radius", method = RequestMethod.GET)
    @ResponseBody
    public String radiusSearch(HttpServletRequest request, Map model) {

        String n1 = request.getParameter("lati");

        return "redirect: /";
    }  

so I am getting the parameter "lati" but it wont redirect the page then I get an error in the second ajax call. 所以我得到参数“ lati”,但它不会重定向页面,然后在第二个ajax调用中出现错误。 Do I need to JSON.Stringify the variables before I pass them? 在传递变量之前是否需要JSON.Stringify变量? I do need to pass the variables "lati","lng","rad" to the api but I was trying to get it working with one first. 我确实需要将变量“ lati”,“ lng”,“ rad”传递给api,但是我试图让它首先与它一起工作。

When you use Ajax, the Spring controller supposes to return text, String, or json. 当您使用Ajax时,Spring控制器假定返回文本,String或json。 why? 为什么?

Because your Ajax success method expects to recieve a text, String, or json. 因为您的Ajax成功方法希望接收文本,字符串或json。

That means you can't perform redirect as an Ajax response . 这意味着您不能将重定向作为Ajax响应执行

So if you want to return the text for lati variable just do: 因此,如果要返回lati变量的文本, lati执行以下操作:

@RequestMapping(value = "/radius", method = RequestMethod.GET)
@ResponseBody
public String radiusSearch(HttpServletRequest request, Map model) {

    String n1 = request.getParameter("lati");

    return n1;
}  

if you want to return a few parameters , then yes, json is a good choice to do it. 如果您想返回一些参数 ,那么可以,json是一个不错的选择。

You can do it easily by using jackson-databind jar, all you need to do is to return an object that contains these 3 variables, and of course you'll need the @ResponseBody annotation on your controller. 您可以使用jackson-databind jar轻松完成此操作,您要做的就是返回一个包含这3个变量的对象,当然,您的控制器@ResponseBody需要@ResponseBody批注。

One more thing, on your client Ajax calls you'll need to mark that you expect to receive a json type. 还有一件事,在客户端Ajax调用上,您需要标记您希望接收json类型。 datatype: "json" ,like this: datatype: "json" ,如下所示:

$.ajax({
        type: "GET",
        url: "http://www.mapquestapi.com/geocoding/v1/address?


        datatype: "json"



key=LIhb6pFxB7qAlFC4Aiul9rM9i7R5BcgB&location=" + address,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-type", "application/json");
        },
        success: function postForm(response) {
            lati = response.results[0].locations[0].latLng.lat;
            lng = response.results[0].locations[0].latLng.lng;

            console.log(lati, lng, rad);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM