json single quotation error - javascript

I want to send JSON data but in value single quotation error
I want this value, but in the value There are single quotes
$.ajax
({
type: "post",
url: "canliAyarlari.aspx/dosyaYaz",
cache: false,
async:false,
data: {"veri:'1. takım kazanır ve maçta 3,5'tan fazla gol olur'}",
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function (durum) {
alert(durum.d);
},
error: function () {
alert("Hata");
}
})

If you ever need to construct a JSON string out of a JS object you always should use JSON.stringify(). That way it takes all the problems with proper encoding of all characters for you.
For your case it will look like:
...
data: JSON.stringify({
veri: "1. takım kazanır ve maçta 3,5'tan fazla gol olur"
}),
...
References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Related

How to pass a single string in AJAX [without key]

I am trying to pass a single quoted string in data without key.
I write a code below :
$.ajax({
type: "POST",
url: 'MY_API_URL',
data: "HATETH",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r);
}
});
it not works..
Please help me to do this. Is it possible or not?

jquery ajax post request fails

trying to send an ajax post request
function ajaxCall(request_data) {
alert(request_data['table'] + request_data['name'] + request_data['description']);
$.ajax({
type: "POST",
cache: false,
url: "../src/api.php/InsertTo",
data: request_data,
dataType: "json",
contentType: 'application/json',
success: function() {
alert('good');
/* $('form').hide();
$('h3').append("Object Successfully Inserted!");*/
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown + textStatus);
}
});
it throws error every time, 'request_data' is an object and url return just a simple string for now, please find the problem
You have to use JSON.stringify() method.
data: JSON.stringify(request_data)
Also, contentType is the type of data you're sending, so application/json; The default is application/x-www-form-urlencoded; charset=UTF-8.
If you use application/json, you have to use JSON.stringify() in order to send JSON object.
JSON.stringify() turns a javascript object to json text and stores it in a string.
Can you try with the below code after using JSON.stringify on the request_data. As per the docs
"The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified."
As you are using dataType: "json" and contentType: 'application/json;' you should convert the javascript value to a proper JSON string.
Please find more in the below link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
function ajaxCall(request_data) {
alert(request_data['table'] + request_data['name'] + request_data['description']);
$.ajax({
type: "POST",
cache: false,
url: "../src/api.php/InsertTo",
data: JSON.stringify(request_data),
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert('good');
console.log(data); // print the returned object
/* $('form').hide();
$('h3').append("Object Successfully Inserted!");*/
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown + textStatus);
}
});

Configure / Change JQuery AJAX Encoding

I am making a basic PATCH call like this one:
.ajax({
url: "/foo/bar/"
type: "PATCH",
dataType: 'json',
data: {
"foo": "foosball",
"bar": "bars on bars"
},
...
});
Jquery automatically encodes the data, which leaves "bars on bars" like "bars+on+bars". Is there a way to change the encoding so that spaces are replaces with %20 rather than pluses?
I've noticed this thread that didn't seem to lead anywhere.
I've also taken note of encodeURI and encodeURIComponent but haven't gotten either to work. Both seem to result in the string being double encoded, leaving me with bars%2520on%2520bars
summary:
What I start with:
... "bars on bars" ...
What the received data looks like after jquery encodes the request:
"bars+on+bars"
What I need the received data to look like:
"bars%20on%20bars"
How about use a variable and pass that to data.
var d={
"foo": "foosball",
"bar": "bars on bars"
}
d.bar=encodeURI(d.bar);
.ajax({
url: "/foo/bar/"
type: "PATCH",
dataType: 'json',
data: d,
...
});
Still wish there was a better way, but I ended up using this plugin from this website.
jQuery.extend({
postJSON: function(url, data, callback) {
return jQuery.ajax({
type: "POST",
url: url,
data: JSON.stringify(data),
success: callback,
dataType: "json",
contentType: "application/json",
processData: false
});
}
});
This forces my api to parse the body as a string into a dict/json. Hopefully in a few years someone can come along with a better solution.

Ajax JSON access array from the array JSON encode server respond

I have an array JSON encode respond when the ajax JSON throw a post request (refer below).
requestparser.php:
$array = array("phweb" => "yes", "phemail" => "yeeess");
echo json_encode($array);
And this Ajax JSON use for sending post request to requestparser.php and processing the return response.
$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]);
alert(result[1]);
}
});
I want to get the value of array key phweb and the value of array key phemail yet when an alert box popup, it says undefined. What seems the problem? Any help, ideas, clues would be greatly appreciated.
So far what I tried is:
$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]->phweb);
alert(result[1]->phemail);
}
});
And sadly, it doesn't work.
The result is a JSON object. You can access it like this
success: function(result) {
alert(result['phweb']);
alert(result['phemail']);
}

jquery Post , data object

I try to understand one thing.
I want to post an object with jquery Ajax POST , something like this:
var dataPostYear = {
viewType:GetViewType(),
viewDate:'2009/09/08',
languageId:GetLanguageId()
};
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
and it doesn't work.
But this one works fine:
var dataPostYear = "{viewType:'"+ GetViewType() + "',viewDate:'2009/09/08',languageId:'"+GetLanguageId()+"}";
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
GetViewType() return --'0'
languageId() return --'1'
it's just a string
there is a way to post an object, something what I try to do in my first way ? Or not ?
Thanks
Use jQuery.param(). Here is the documentation
You should look at .postJSON.
Essentially, you just add json as a 4th argument to the $.post
From the site:
// Send the request
$.post('script.php', data, function(response) {
// Do something with the request
}, 'json');
If you want the .ajax call version, you can convert it using the .post docs.

Categories

Resources