简体   繁体   English

使用AJAX将JSON数据从Google Maps传递到MVC控制器

[英]Pass json data from Google Maps to MVC controller with AJAX

I want to store data about addresses and coordinates of markers on map, so I'm creating button in Infowindow which will redirect me to form on another view (also with another controller). 我想存储有关地图上标记的地址和坐标的数据,所以我在Infowindow中创建按钮,它将重定向我到另一个视图上的表单(也可以使用另一个控制器)。 I want this form to be already filled with data about coordinates and address. 我希望此表单已经充满有关坐标和地址的数据。 I have function called on button click with AJAX code in it to send JSON data to method in controller. 我在按钮单击时调用了带有AJAX代码的函数,以将JSON数据发送到控制器中的方法。 Problem is that after clicking on button controller method isn't called (although function call works properly while debugging). 问题是单击按钮控制器后方法没有被调用(尽管函数调用在调试时可以正常工作)。 I've been searching a lot for a solution, but I really don't know what did I do wrong. 我一直在寻找解决方案,但是我真的不知道我做错了什么。

Call to addMarker : 调用addMarker

google.maps.event.addListener(marker, 'click', function (event) {
            if(infowindow) infowindow.close();
            infowindow = new google.maps.InfoWindow({content: data});
            infowindow.open(map, marker);

            var buttonAdd = document.getElementById('addButton');

            buttonAdd.onclick = function() {
                addMarker(event.latLng, address);
            }
        }); 

JS function with AJAX: 带有AJAX的JS函数:

   function addMarker(location, fulladdress) {
        var data = JSON.stringify(fulladdress + location) //it's ok, i checked with firebug

        $.ajax({
            type: "POST",
            url: "Incidents/Create",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: data
        })
    }

Controller: 控制器:

public class IncidentsController : Controller
    {
        //some code
        [HttpPost]
        public ActionResult Create(string JsonStr)
         {
             return View();
         }
         //some code
    }

For now I'm just trying to get to another view without doing anything with recieved data, but it's not working. 目前,我只是想在不对接收到的数据进行任何处理的情况下进入另一个视图,但是它不起作用。 There's definetely something wrong with ajax or controller. ajax或controller肯定有问题。 I'm new to MVC, JS and AJAX, so please forgive me if it's something stupid. 我是MVC,JS和AJAX的新手,所以如果这很愚蠢,请原谅我。 Thanks in advance. 提前致谢。

TL;DR - Clicking on button should result in recieving another view with partially filled (with address and coordinates) form. TL; DR-单击按钮应该会导致接收到另一个视图,其中包含部分填充的表单(包括地址和坐标)。

Found the problem. 找到了问题。

You are using dataType: "json" . 您正在使用dataType: "json" If you want to post JSON in MVC then you need to create appropriate model with the same format that you are going to post. 如果要在MVC中发布JSON ,则需要以将要发布的相同格式创建适当的模型。

Take a look at below example. 看下面的例子。

Controller : 控制器:

 public class JsonPostExampleController : Controller
 {
     [HttpPost]
     public ActionResult JsonPost(JsonModel data)
     {
          return View();
     }
 }

Javascript : Javascript:

$.ajax({
    type: "POST",
    url: "JsonPostExample/JsonPost",
    dataType: 'json',
    data: { 'name': 'ravi' },
    success: function (data) { }
});

Model : 型号:

public class JsonModel
{
    public string name { get; set; }
}

Now as per your requirement, we can not use dataType: "json" as you can't create model according to google's response format for fulladdress and location. 现在,根据您的要求,我们无法使用dataType: "json"因为您无法根据google的响应地址格式为fulladdress和location创建模型。

So you need to use dataType: "text" and for that you only need to modify your javascript. 因此,您需要使用dataType: "text" ,为此,您只需要修改javascript。

Update your javascript block with below : 使用以下更新您的JavaScript块:

function addMarker(location, fulladdress) {
    var data = JSON.stringify(fulladdress) + JSON.stringify(location)

    $.ajax({
        type: "POST",
        url: "Incidents/Create",
        dataType: "text",
        data: { JsonStr : data }
    });
}

and your controller code remains same as : 并且您的控制器代码仍然与:

public class IncidentsController : Controller
{
    //some code
    [HttpPost]
    public ActionResult Create(string JsonStr)
    {
        return View();
    }
    //some code
}

now you should be able to get your data in string format at server side. 现在您应该能够在服务器端以字符串格式获取数据。 If you want JSON server side then you can parse the string. 如果您想要JSON服务器端,则可以解析该字符串。

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

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