MVC2 JSON input - json

I have a problem with a simple VS2010 .NET 4.0 MVC2 application.
My controller action looks like this:
public JsonResult GetJson(string query)
I access the action with jQuery like so:
function getJson() {
var postdata = {};
postdata['query'] = $('#query').val();
$.ajax({
type: "POST",
url: '<%= Url.Action("GetJson") %>',
data: JSON.stringify(postdata),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
The action is executed upon the jQuery XHR request, but no matter what the "query" value is ALWAYS null. I can view the POST request/response in Firebug, and it shows the proper JSON string being sent to the action.
What could the problem be? It just seems like MVC is not parsing/deserializing the JSON input at all.
Thanks!

Please try this as it should work for you:
$.ajax({
type: "POST",
url: '<%= Url.Action("GetJson") %>',
data: {query: $('#query').val()},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
}
});

Related

Json does not write the returned object to the console

I have this piece of code that consumes a restful API using ajax. I am just trying to write on the console the returned data from the API, to my surprise it does not show any data, what's to be corrected?
$.ajax({
type: "get",
url: "http://localhost:8800/dialects",
crossDomain: true,
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
When I query the endpoint directly from the browser it returned the object's endpoint: http://localhost:8800/dialects. Sample of returned objects:
[{"glottocode":"zyud1238","names":"Zyuzdin","isocodes":"","macroarea":"Eurasia"},{"glottocode":"zwal1238","names":"Zwall","isocodes":"","macroarea":"Africa"},{"glottocode":"zuwa1238","names":"Zuwadza","isocodes":"","macroarea":"Papunesia"},{"glottocode":"zuti1239","names":"Zutiua","isocodes":"","macroarea":"South America"},]
Try using content-type attribute in ajax
$.ajax({
url: "http://localhost:8800/dialects",
type: "get",
crossDomain: true,
contentType: "application/jsonp; charset=utf-8",
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});

JSON API to HTML

I try using different instructions to use a JSON API from a Wordpress-System in a HTML-Teamplate. Unfortunately I do not succeed. Does anyone have any idea how I can read the section "Content" of http://www.earnyour21.de/api/get_page/?id=1588?
blog: function () {
$.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
}
If the data structure of the JSON will always be the same, you can simply access the object directly using the objects name in JS.
blog: function(){
$.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function(data){
$('#content_test').append(data['page']['content']);
},
error: function(data){
$('#content_test').append(data['page']['content']);
}
});
}
Basically you need to use jquery to grab the div with an id of content_test and then append your data from the json. http://api.jquery.com/append/ and http://www.json.com/ for further reference.

JSON data not parsing in node.js displays undefined in console

JSON data not parsing in node.js displays undefined in console..
Here is HTML code:
var jsonObjects = [{id:1, name:"amit"}];
jQuery.ajax({
url: 'http://localhost:8081',
type: "POST",
data: {"id":"1", "name":"amit"},
dataType: "json",
success: function(result) {
alert("done")
}
});
Here is Nodejs code:
http.createServer(function (request, response)
{
response.writeHead(200, {"Content-Type":"text/plain"});
var urlObj = JSON.stringify(response.data, true);
console.log(urlObj)
response.end();
}).listen(8081);
Try using GET method Instead of Post. Try this
var jsonObjects = [{id:1, name:"amit"}];
$.ajax({
type: 'GET',
url: 'http://localhost:8081',
data: {
jsonData: JSON.stringify(jsonObjects)
},
dataType: 'json',
complete: function(validationResponse) {
}
});
The data will be held in the request, not the response as it has come from the client request.
If you are using express to create your http server you will also need to tell it to use the bodyParser middle wear.

data is not transformed well to querystring parameter when making api request using jQuery ajax call

$.ajax({
type: "GET",
async: false,
url: "http://localhost:1234/api/",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{"id":"125"}',
success: function (data) {
console.log(data.toString());
}
});
When I call api using jQuery ajax call, data that I pass is not transformed well to querystring parameter.
here is what I see that's being called.
http://localhost:1234/api/?{%22id%22:%22125%22}
but when I pass data as an object for example, data: {"id":"125"} is works fine. What am I doing wrong here?
You pass data as a js Object, and jQuery is converting it to JSON format, because you specify the JSON dataType.
$.ajax({
type: "GET",
async: false,
url: "http://localhost:1234/api/",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {"id": 125},
success: function (data) {
console.log(data.toString());
}
});

JSON Redirect from MVC Controller not working

I've tried the steps outlined in other posts here
and here and it just doesn't work. I end up getting redirected to a white screen that just says ... {"redirectTo":"/Administrator/Home"}
C#
[HttpPost]
public JsonResult ControllerMethodHere(ViewModel model) {
// my controller code goes here.
return Json(new {
redirectTo = Url.Action("Index", "Home"),
}, JsonRequestBehavior.AllowGet);
}
Javascript
this.save = function () {
$.ajax({
url: $('form').attr('action'),
type: "POST",
data: ko.toJSON(this),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
window.location.href = data.redirectTo;
}
});
};
Try using this:
window.location = data.redirectTo;