Converting $.post into $.ajax - javascript

I have a post statement,
$.post("panel.php", 'data=[{"action":"UserInfo"}]', function (userInfo){
//processing
});
I need it to be converted to $.ajax so made it thus,
$.ajax({
type: "POST",
url: "panel.php",
data: { data: [{"action":"UserInfo"}]},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
But the post variable isn't being sent. Is this not the correct way?

Can you try something like this:
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
//processing
}
});

Try this
$.ajax({
type: "POST",
url: "panel.php",
data: "action="+"UserInfo",
success: function(userInfo) {
//processing
}
});

Remove data from your data and keep it in a variable, stringify before your send as below
var data={"action":"UserInfo"};
$.ajax({
type: "POST",
url: "panel.php",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});

Your data attribute was not written as correct JSON:
data: { "data": [{"action":"UserInfo"}]},
You need quotation marks around the items inside your JSON object. You can use JSONLint to check if your JSON object is valid.

$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
dataType: "json",
success: function(userInfo) {
//processing
}
});
Need a small change. there is a predefined format to send data in ajax,
data: {status: status, name: name},
data: "status="+status+"&name="+name.
Follow any one of the approach.

try like this,
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
}
});

Related

get JSON data from a web-service to make a picture gallery

A web-service is available at http://www.cs.sun.ac.za/rw334/products.php?
limit=12&skip=0
I want to get the data in there into a Javascript array. I've searched around and I think I should start like this?
$.ajax({
type: "GET",
url: "http://www.cs.sun.ac.za/rw334/products.php?limit=12&skip=0",
data: {id:id, name:name,url:url},
contentType: "application/json",
dataType: "json",
success: ??
}
});
How should I continue to get this data from the .php file into a Javascript array?
product = [];
$.ajax({
type: "GET",
url: "http://www.cs.sun.ac.za/rw334/products.php?limit=12&skip=0",
dataType: "json",
success: function(data) {
$(data.products).each(function(i, products){
product[products.id] = products.name;
});
console.log(product);
}
});

Make AJAX post call with data

This is in my default.aspx
$.ajax({
url: "Default.aspx/Myfunction",
dataType: "json",
type: "POST",
data: {someParameter: "some value"},
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
error: function (d) {
alert("error");
}
});
And this is in my codebehind:
[WebMethod]
public static string Myfunction(string someParameter)
{
return "hello";
}
It keeps going to the error. I see that if I send the Ajax request with null for data and no parameters on the function I get the data "hello" out. So there is some issue in how I send the data, but it is unclear what
Put your parameters in quotes
$.ajax({
url: "Default.aspx/Myfunction",
dataType: "json",
type: "POST",
data: {'someParameter': 'some value'},
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
error: function (d) {
alert("error");
}
});
Data object must be a single string.
data: JSON.stringify({someParameter: "some value"})
Change this line:
data: {someParameter: "some value"},
to:
data: {"someParameter": "some value"},
JSON object properties needs to be enclosed with quotes.
Here is how I do the same thing:
$.ajax({
type: "POST",
dataType: "json",
url: "RouteService.asmx/getRouteData",
data: { techID: techID, jobID: jobID },
success: function(msg) {
processRouteData(msg);
}
Try removing this line:
contentType: "application/json; charset=utf-8",

Ajax post doesn't pass any data to MVC controller

This:
$.ajax({
url: '/Merchant/SaveDirty',
type: 'POST',
dataType: 'json',
data: ko.toJSON(dirtyItems),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// get the result and do some magic with it
//var message = data.Message;
alert(ko.toJSON(dirtyItems));
}
});
...currently calls this:
[HttpPost]
public void SaveDirty(string json)
{
}
...but when I hit the breakpoint in SaveDirty, no data is passed. I've verified that ko.toJSON(dirtyItems) returns a JSON string in the javascript. What am I doing wrong?
Thanks!
#KillingsWorth, is there any specific reason for which you are posting a JSON string? If not then, you could create a class corresponding to dirtyitems type and in your controller method you can accept a list of dirtyItems.
Class DirtyItem
{ // dirty item properties }
[HttpPost]
public void SaveDirty(List<DirtyItem> dirtyItems)
{
}
you can use the following:
$.ajax({
url: '/Merchant/SaveDirty',
type: 'POST',
dataType: 'json',
data: JSON.stringify(dirtyItems),
contentType: 'application/json; charset=utf-8',
success: function (data) {
///
///
}
});
But if you are using knockout.js in your applicantion then you should do the following:
$.ajax({
url: '/Merchant/SaveDirty',
type: 'POST',
dataType: 'json',
data:JSON.stringify(ko.mapping.toJS(dirtyItems)),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// get the result and do some magic with it
//var message = data.Message;
alert(ko.toJSON(dirtyItems));
}
});
This should works:
$.ajax({
url: '#Url.Action("SaveDirty", "Merchant")'
type: 'POST',
dataType: 'json',
data: JSON.stringify(dirtyItems),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// get the result and do some magic with it
//var message = data.Message;
alert(ko.toJSON(dirtyItems));
}
});

AJAX POST JSON value

I have AJAX POST, the result is JSON:
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var upload = JSON.stringify(result);
console.log(upload);
}
});
The upload result is:
{"Link":0,"Title":"d","Description":"dada","Keywords":"dad"}
How can I get the value of Title?
Do not stringify the result, just use result.Title.
As you already have JSON string, It's simple as a pie!
All you need to do is to call the property you want from the variable you assigned your result to.
for example:
var post_response;
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
post_response = JSON.stringify(result);
console.log("Title: "+post_response.Title);
}
});
hope this helps.

getting xml post data in a function called with ajax

I have
$.ajax({
type: "POST",
dataType: "xml",
url: getUrl('/GetPeriodicStats/'),
data: XML.innerHTML,
success: function(c)
{
I need to get XML.innerHTML in GetPeriodicStats.
I know it's supposed to be in $_POST variable, but what index do I write for it ?
I tried $_POST['data'], but it's not good...
What should I write in $_POST[???] ?
Try this
$.ajax({
type: "POST",
dataType: "xml",
url: getUrl('/GetPeriodicStats/'),
data: {xml: XML.innerHTML},
success: function(c)
{
Then that data should be available in $_POST['xml']

Categories