line-based when sending a JSON through an ajax call - json

I need to perform an AJAX call to send some JSON object to a server
$.ajax({
url: serviceURL+'stores/'+store_id+'/karaoke/song/new',
type: "POST",
contentType: "application/json; charset=utf-8",
data: { song: id, date: "date", user: "test", help: false, partners: [], likes: 0 },
dataType: "json"
});
that's what i got, I used wireshark to log what i was sending and what i was sending was this
JavaScript Object Notation: application/json
Line-based text data: application/json
song=name&date=date&user="test"&help=false&partners=[]&likes=0
How could that call, with the specified dataType and contentType could transform a JSON into line based text data, how can i send the JSON object instead

You can use the Json2 library to convert the json to a string before it is sent. Once it reaches the server it will be parsed.
var song = { song: id, date: "date", user: "test", help: false, partners: [], likes: 0 }
var json = JSON.stringify(song)
$.ajax({
url: serviceURL+'stores/'+store_id+'/karaoke/song/new',
type: "POST",
contentType: "application/json; charset=utf-8",
data: json,
dataType: "json"
});

Related

Symfony $request->getContent() in bad format?

i send a request to my symfony backand via
$.ajax({
url: "/example/example",
method: "POST",
data: { test: "hallo", test2 : "hallo2" },
contentType: "application/json; charset=utf-8",
dataType: "json"
...
but my i become in controller when i use $request->getContent() the following output:
test=hallo&test2=hallo
but i need the json formatted content, how i can get this from the request? Like so:
[
{"test" : "hallo"},
{"test2" : "hallo2"}
]
You are not sending JSON data to your backend, you can use JSON.strinify to do this.
var data = [
{"test" : "hallo"},
{"test2" : "hallo2"}
];
jsonData = JSON.stringify(data);
$.ajax({
url: "/example/example",
method: "POST",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json"
...

Sending a post request through ajax with data as json, but in the endpoint it is not coming as json

I am building a simple post request using ajax and nothing fancy about that:
$.ajax({
type: 'POST',
contentType: "application/json",
dataType: 'json',
url: 'https://nnn.beeceptor.com',
data:{
country: "country",
customer_title: "customer_title",
date_of_birth: "date_of_birth",
education: "education",
email: "email",
first_name: "first_name",
institution: "institution",
last_name: "last_name",
pay_type: "pay_type",
phone: "phone",
way_of_contact: "way_of_contact",
work_experience: "work_experience"
},
success: function(data){
console.log(`success`)
},
error: function(data){
console.log(`error`);
}
})
Now I want in the endpoint that data that I am sending in the body to come as json.
How can I fix that so the data comes to the endpoint formatted as json. For tests you can use that endpoint to see the format in which the data is coming. Any suggestion is much appreciated.
After some research I was able to come with an answer. On the ajax request was needed to define the content in the header as follows:
headers: {
"content-type": "application/json;charset=UTF-8" // Add this line
},
and in the data attribute:
data: JSON.stringify(data_object)
That solved my problem

WinJs xhr getting error The request entity's media type 'text/plain' is not supported for this resource

WinJs xhr getting error The request entity's media type 'text/plain' is not supported for this resource,
I am sending json to a webapi , I have set the header to application/json so I was not expecting this error.
any help here thanks
WinJS.xhr(
{ url: "http://api.xxxxxx.com.au/api/jobs/GetNearActiveJobs", type: "POST", responseType: "json", data: { LocationId: 23555, kms: 10 }, headers: { contentType: "application/json" } } )
Try this
WinJS.xhr({
type: "post",
url: "http://api.xxxxxx.com.au/api/jobs/GetNearActiveJobs",
headers: { "Content-type": "application/json" },
data: JSON.stringify({ LocationId: 23555, kms: 10 })
})

Why is the update operation not posting any data?

I am using the Kendo Grid with inline editing. When I click the "Update" button, a POST gets made to my controller method with this signature. The controller action gets hit, so the POST is working.
[HttpPost]
public HttpResponseMessage SaveAccountAdmin(string jsonCompanyContacts)
However the POST data in the update operation never arrives - its always null.
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
data: {
jsonCompanyContacts: "John Doe"
}
},
Here is the FULL data source code.
var dataSource = new kendo.data.DataSource(
{
batch: false,
pageSize: 10,
transport: {
create: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json"
},
read: {
url: "/Company/ReadAccountAdmin"
},
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
data: {
jsonCompanyContacts: "John Doe"
}
},
//destroy: {},
parameterMap: function (data, type) {
return kendo.stringify(data);
}
},
this doesnt work either:
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
//data: { "jsonCompanyContacts": kendo.stringify({ jsonCompanyContacts: "John Doe" }) }
data: { "jsonCompanyContacts": "John Doe" }
},
//destroy: {},
parameterMap: function (data, type) {
return kendo.stringify(data);
}
BUT THIS WORKS- WHY?
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
//data: { "jsonCompanyContacts": kendo.stringify({ jsonCompanyContacts: "John Doe" }) }
//data: { "jsonCompanyContacts": "John Doe" }
},
//destroy: {},
parameterMap: function (data, type) {
return kendo.stringify({ "jsonCompanyContacts": "John Doe" });
}
The value is not passed to the controller as a string. Try using a model. This might help: MVC3 & JSON.stringify() ModelBinding returns null model
UPDATE
You really don't want to do it like that. Might work in theis one case, but you are shooting yourself in the foot.
Model
public class CompanyContactModel
{
public string CompanyContacts { get; set; }
}
Controller Signature
public JsonResult SaveAccountAdmin(CompanyContactModel companyContactModel)
...
Better
public JsonResult SaveAccountAdmin([DataSourceRequest]DataSourceRequest request, CompanyContactModel companyContactModel)
...
Update and Return and put into List
If error: ModelState.AddModelError(string.Empty, e.Message);
DataSourceResult result = [Your Model List].ToDataSourceResult(request, ModelState);
return Json(result, JsonRequestBehavior.AllowGet);
}
Try doing this in your update definition:
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
data:{ "jsonCompanyContacts": kendo.stringify({ jsonCompanyContacts: "John Doe" })}
}
You might have to remove the operation in your parameterMap for this to work. The main thing is that you want to post a variable with the same name as in your controller. That variable should contain your stringified data.
You could also move this operation to your parameterMap if you want.
I had similar problem like you.I am getting values from the broswer , but its not posting the values to the update action model.
In my case I used "[ScriptIgnore(ApplyToOverrides = true)]" on the model which works fine.

how connect with web service

I am trying to get some data from a web service:
$.ajax({
type: 'POST',
url: "http://192.******.asmx?op=GetJSONString",
method: "serverCallback",
data: "Select con.cod as codigo, con.res as descripcion from con where ide>0",
success: function(data){alert(data)},
});
How can I get the returned JSON data?
Here is what my calls look like. Can you be more specific of the issue you are having?
$.ajax({
type: "POST",
data: "SOME DATA",
dataType: "json",
url : "/myapp/service.json",
cache: false,
error: function() {
alert("There was an issue");
}
,
success: function(data)
{
processJson(data);
}
});
I will also post what has worked with me for asmx services.
In the example below, for "url": "/service/Details.asmx/Reject", the "/Reject" is the method name in the web service (asmx) file.
$.ajax({
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "POST",
"url": "/service/Details.asmx/Reject",
"data": "{\"itemId\":\"" + id + "\",\"comment\":\"" + comms + "\"}",
"success":
function (msg) {
alert("Yea it worked!");
});
}
});