javascript - Pass json data from Google Maps to MVC controller with AJAX -
i want store data addresses , coordinates of markers on map, i'm creating button in infowindow redirect me form on view (also controller). want form filled data coordinates , address. have function called on button click ajax code in send json data method in controller. problem after clicking on button controller method isn't called (although function call works while debugging). i've been searching lot solution, don't know did wrong.
call 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 ajax:
function addmarker(location, fulladdress) { var data = json.stringify(fulladdress + location) //it's ok, checked 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 i'm trying view without doing recieved data, it's not working. there's definetely wrong ajax or controller. i'm new mvc, js , ajax, please forgive me if it's stupid. in advance.
tl;dr - clicking on button should result in recieving view partially filled (with address , coordinates) form.
found problem.
you using datatype: "json". if want post json in mvc need create appropriate model same format going post.
take @ below example.
controller :
public class jsonpostexamplecontroller : controller { [httppost] public actionresult jsonpost(jsonmodel data) { return view(); } } 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 per requirement, can not use datatype: "json" can't create model according google's response format fulladdress , location.
so need use datatype: "text" , need modify javascript.
update javascript block below :
function addmarker(location, fulladdress) { var data = json.stringify(fulladdress) + json.stringify(location) $.ajax({ type: "post", url: "incidents/create", datatype: "text", data: { jsonstr : data } }); } and controller code remains same :
public class incidentscontroller : controller { //some code [httppost] public actionresult create(string jsonstr) { return view(); } //some code } now should able data in string format @ server side. if want json server side can parse string.
Comments
Post a Comment