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.
Related
I want to send data to a camera, and it only accepts the data when it has no encoding to it.
The data should look like
<?xml version="1.0" encoding="UTF-8"?>
<EventTriggerList version="2.0">
...
</EventTriggerList>
I have a function that creates that string just fine, and when I use it via an application to send to the camera, it works. But when I use Ajax:
$.ajax({
url: target,
type: 'PUT', //type is any HTTP method
contentType: "application/xml",
data: {
data: dataString
},
success: function () {
}
})
;
it sends the above as
<?xml+version="1.0"+encoding="UTF-8"?>
<EventTriggerList+version="2.0">
...
</EventTriggerList>
and the camera won't accept it.
I tried setting processData to false, but then my payload is only 15 bytes, instead of the expected 300 bytes of string.
How do I get the payload to be precisely as the string I generated?
Since your content type is xml and dataString is also xml, just pass it as is
$.ajax({
url: target,
type: 'PUT', //type is any HTTP method
contentType: "application/xml",
data: dataString,
success: function () {
}
})
I have a function in my javascript that returns a json. I want to get the return value of that javascript function using c# so I can convert it in dataTable. Can anyone help me? Sorry I'm just a beginner in asp.net webforms.
JS
function getAllMessages()
{
$.ajax({
url: '/api/Messages/',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'JSON'
}).success(function (result) {
});
}
since you don't have the actual code , i'll just give an algorithm. I experienced this scenario in one of my projects.
In your html/asp.net button, execute a webmethod that has a parameter to receive the json format
$.ajax({
type: 'POST',
url: 'YourPage.aspx/c#MethodThatWillReceiveJSON',
data: "{c#MethodParameterName:" + JSON.stringify(YourJSONvalues) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (r) {
alert(r.d.exampleDataFromJson);
}
});
You need to create another javascript function that will call your original javascript (The one who will return the json format) and use that function to pass it as an argument to the parameter of your c# webmethod.
//your original javascript function that returns json
function getJsonData()
{
// your code
}
// use that js function to store in a var , then pass it to your c#
var MyJSONdata = getJSONData();
$.ajax({
type: 'POST',
url: 'YourPage.aspx/c#MethodThatWillReceiveJSON',
data: "{c#MethodParameterName:" + JSON.stringify(MyJSONdata) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (r) {
alert(r.d.exampleDataFromJson);
}
});
If you use a String type as a parameter in your c# webmethod, you can not use/manipulate it as a json stuff, you need to convert your String variable using json serializer :) which you can find a lot of reference here in net/stackoverflow, it is just one liner of code to convert. :)
I'm trying to convert the curl code from an API called TextRazor to jquery's AJAX because of a platform limitations. I have tried many solutions from similar questions by the community but can't seem to get any data back (through the alert dialog). If it matters
from the documentation calling the API looks like this:
curl -X POST \
-H "x-textrazor-key: YOUR_API_KEY" \
-d "extractors=entities,entailments" \
-d "text=Spain's stricken Bankia expects to sell off..." \
https://api.textrazor.com/
My current AJAX code looks like this:
$.ajax({
url: "https://api.textrazor.com/",
type: "POST",
dataType: 'json',
data: {
x-textrazor-key: "YOUR_API_KEY",
extractors: "entities,entailments",
text:"Spain's stricken Bankia expects to sell..."
},
success:function(data) {
alert(JSON.stringify(data));
},error: function(xhr) {
alert("<some error>");
console.error(xhr.responseText);
}});
here is the link to jsfiddle if it helps: jsfiddle.net
Thanks for your support!
I think you have to pass "x-textrazor-key: YOUR_API_KEY" as additional header
$.ajax({
url: "https://api.textrazor.com/",
type: "POST",
dataType: 'json',
beforeSend: function(xhr){xhr.setRequestHeader('x-textrazor-key', 'YOUR_API_KEY');},
data: {
extractors: "entities,entailments",
text:"Spain's stricken Bankia expects to sell..."
},
success:function(data) {
alert(JSON.stringify(data));
},error: function(xhr) {
alert("<some error>");
console.error(xhr.responseText);
}});
data: {
x-textrazor-key: "YOUR_API_KEY",
The data: bracket in jQuery means that you want to send that data as POST, while you need to send the API key as a header.
Add this field to your code (after URL or so):
headers: {"x-textrazor-key": "YOUR_API_KEY"}
This looks close to me, but you put the header into the POST body. I think it should be the below. (Note that you also need quotes around 'x-textrazor-key', since the dashes in it will otherwise be interpreted as subtraction.)
$.ajax({
url: "https://api.textrazor.com/",
type: "POST",
dataType: 'json',
headers: {
'x-textrazor-key': "YOUR_API_KEY"
},
data: {
extractors: "entities,entailments",
text: "Spain's stricken Bankia expects to sell..."
},
success: function (data) {
alert(JSON.stringify(data));
},
error: function (xhr) {
alert("<some error>");
console.error(xhr.responseText);
}
});
There could of course be other issues here. (E.g. perhaps the API doesn't support cross-origin requests.) You'll want to take a look at the network tab in your browser's developer tools to see what actually happens.
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
I am having problem displaying accented character in my app; It is showing ⛽ instead of ó. The string is coming from a json file retrieved from a server. Here are the technical details:
JSON: (This is the object being retrieved from the server)
notice the 3rd key "Relación" the letter "o" is accented.
[
{
"key": "Canales"
},
{
"key": "Productos"
},
{
"key": "Relación con el ejecutivo"
}
]
Ajax (here is the code to retrieve the information)
notice I already added charset=utf-8 as most answers suggest
$.ajax({
url: url,
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(uri){
alert("clintg test: " + JSON.stringify(uri));
}
}
Alert: (as you can see, it is just showing a box symbol where it's supposed to be -> ó)
To give more detail to #georg 's advice that helped me solve this issue:
Since I can't change the server side scripts, I adjusted the code on my side.
I changed the charset in my ajax request to ISO-8859-1, but since the default charset of ajax is utf-8, I had to override the charset with $.ajax.beforeSend:
$.ajax({
url: url,
type: "GET",
dataType: "json",
contentType: "application/json; charset=iso-8859-1",
success: function(uri){
alert("clintg test: " + JSON.stringify(uri));
},
beforeSend: function(jqXHR) {
jqXHR.overrideMimeType('application/json;charset=iso-8859-1');
}
}
Here's a link to the question that helped me figure out and override the charset: Jquery ignores encoding ISO-8859-1