how to get the json data from the url in jquery? - ajax

How to get the json array from the URL.I declare the mydata as a variable.how to get the json array into the mydata? using the following ajax
var mydata;
$.ajax({
url: someurl,
dataType: 'json',
success: function() {
}
});

var mydata;
$.ajax({
url: someurl,
dataType: 'json',
success: function(data) {
mydata = data;
console.log(mydata);
}
});
alternatively you could use $.getJSON() function

Try this:-
***$.ajax({
url: someurl,
dataType: 'json',
success: function(mydata) {
foreach(var data in mydata){
//do something.....
}
},
failure: function()
{
//error message
}
});***

Related

ajax POST int parameter in asp.net core

I am migrating my MVC project to Core and I have been having a hard time fixing all the old ajax calls.
I can pass a model and string parameters into the controller, however, ints are not working for me.
I can wrap them into a JSON object as a string parameter such as [FromBody]string objId in the controller, but then I have to parse the int val from the Json {'objId' : 1}.
Is there a way I can avoid this and just pass an int?
below is the code I am trying.
[HttpPost]
public JsonResult PassIntFromView([FromBody]int objId)
{
//DO stuff with int here
}
here is the js.
var data = { "objId": 1};
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
data: JSON.stringify(data),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
The objId is always 0 in the controller.
I have tried this without doing JSON.stringify(data) as well with no result.
I have also tried all the different form attribute variations.
Try to use contentType as 'application/x-www-form-urlencoded':
var data = { objId: 1 };
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
type: "post",
contentType: 'application/x-www-form-urlencoded',
data: data,
success: function (result) {
console.log(result);
}
});
Then remove the [FromBody] attribute in the controller
[HttpPost]
public JsonResult PassIntFromView(int objId)
{
//Do stuff with int here
}
I believe your issue could be that you are passing an object to the api, but trying to turn it into a primitive. I know there is already a chosen answer, but give this a whirl.
var data = { };
data["objId"] = 1; //I just wanted to show you how you can add values to a json object
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
data: JSON.stringify(data),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
You create a model class
public class MyModel {
public int ObjId {get;set;}
}
Your controller should expect one of these
[HttpPost]
public JsonResult PassIntFromView([FromBody] MyModel data)
{
//DO stuff with int here
}
JSON has a preference for strings not integers. You are better off to use JSON.stringify(data) to parse to your controller, convert that to a integer in the controller, then parse the string that was returned as:
var data = { objId: 1};
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',//asp.net - url: 'api/controllerName/controllerFunction'
data: JSON.stringify(data),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
var result = JSON.parse(data);
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
Try this:
var data = { "objId": 1};
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
data: data,
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
Your controller:
[HttpPost]
public JsonResult PassIntFromView(int objId)
{
//DO stuff with int here
}
Js
var data = { "objId": 1};
$.ajax({
url: "ControllerName/PassIntFromView",
data: data,
type: "POST",
dataType: 'JSON',
success: function(data.result!=null) {
console.log(data.result);
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
I got it working like this
let numberFour = JSON.stringify(4);
$.ajax({
.......
.......
data: numberFour,
type: "POST",
dataType: 'JSON',
contentType: "application/json",
......
........
});

jsgrid returns blank line after update

After giving an update via ajax in jsgrid the line returns blank
controller: {
loadData: function(filter) {
var data = $.Deferred();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/app/Play/",
dataType: "json"
}).done(function(response) {
data.resolve(response);
});
return data.promise();
},
updateItem: function(item) {
return $.ajax({
type: "POST",
url: "/app/Play/change.php",
data: item,
});
},
}
In the return of the url update: /app/Play/change.php
I return the updated record data in a normal json, same as the initial json used in the load
Resolved:
controller: {
loadData: function(filter) {
var data = $.Deferred();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/app/Jogos/app/",
data: filter, // <--Insert line, this is a problem
dataType: "json"
}).done(function(response) {
data.resolve(response);
});
return data.promise();
},
updateItem: function(item) {
return $.ajax({
type: "POST",
url: "/app/Jogos/app/change.php",
data: item
});
},
},

Cant access spotify object

i want to display the albums of an artist that i passed from my api controller
the request is here.
I can access the info object, but when i access the albums object it returns undefined
<script>
$(function () {
$('table tr').click(function () {
var id = this.id;
$.ajax({
type: "GET",
url: '/api/author/GetName/' + id,
contentType: "text/plain charset-utf-8",
data: id,
dataType: "json",
success: function (data) {
getDetails(data.name);
}
});
}
);
});//End ready
function getDetails(art) {
$.ajax({
type: "GET",
url: 'http://ws.spotify.com/search/1/track.json?q='+ art ,
dataType: 'json',
success: function (data) {
$('#summaryDisplay').empty();
$('#summaryDisplay').append((JSON.stringify(data.albums)) + '<br/>');
alert(JSON.stringify(data.info));
},
error: function (data) {
$('#summaryDisplay').html('<h3>Error in retrieval</h3>');
}
});
}
You are accessing the wrong URL in your code. Use album instead of track.
{
// [...]
url: 'http://ws.spotify.com/search/1/album.json?q='+ art,
// [...]
}

Pass array to ajax request in $.ajax()

I have tried like this but could not get array list. It returns 'null'
var data=[];
data[0] = '1';
data[1] = '2';
$.ajax({
url: '#Url.Action("AddFrequencyWeekly", "Plannar")',
type: "POST",
data: { data: data },
dataType: 'json',
success: function (data) {
alert("Record Updated Successfully");
}
});
my model class code following
public JsonResult AddFrequencyWeekly(string[] data)
{
}
data value says 'null'
please help me?
var datas = { data0: '1',
data1: '2' };
$.ajax({
url: '#Url.Action("AddFrequencyWeekly", "Plannar")',
type: "POST",
data: datas,
dataType: 'json',
success: function (data) {
alert("Record Updated Successfully");
}
});
Its the way you are expecting to recive the data in the action method,
try
public JsonResult AddFrequencyWeekly(IEnumeable<string> data)
{
}

display values returned by json

I have simple question to display data on html page. Following code displays array of json data on screen. but, I want to display it by each element such as "url", "img_url" and so on.
could you please let me know who to do it ?
ajax code
var dataString = 'url=' + pathname + '&img_name=' + img_name + "&tag=" + tag;
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
complete: function (xhr, status) {
if (status === 'error' || !xhr.responseText) {
//handleError();
alert("error");
} else {
var data = xhr.responseText;
$('#tt').html("<div id='message'></div>");
$('#message').html(data);
}
}
});
json return
{"cid":"14","url":"http:\/\/localhost\/","img_url":"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg","img_name":"mat_173317134c.jpg","html":"<div id=\"hotspot-19\" class=\"hs-wrap hs-loading\">\r\n<img src=\"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg\">\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"95\" data-y=\"64\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nasdf\r\n<\/div>\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"168\" data-y=\"53\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nrere\r\n<\/div>\r\n<\/div>\r\n","jscript":""}
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
success: function (data) {
for(var item in data){
console.info(item);//print key
console.info(data[item]);//print value
}
}
});
I hope this is what you need.

Resources