I successfully posy via ajax. However, I realized that it doesnt post "&". It also doesnt give any error. Here is my script
Html
<textarea id="aciklamatext">West & Union</textarea>
Ajax post
var aciklama = $('#aciklamatext').val();
$.ajax({
type: "POST",datatype:"json", async: false,
contentType: "application/x-www-form-urlencoded",
url: "/aciklama.php",
data: "aciklama=" + aciklama,
success: function(html){
}
});
aciklama.php
$aciklama = $_POST["aciklama"];
echo $aciklama;
Output
West
The easiest way to send data by ajax is to use JSON. Internal ajax converts the JSON data to a string and encodes all the special chars:
$.ajax({
type: "POST",
datatype:"json",
url: "/aciklama.php",
data: {"aciklama": + $('#aciklamatext').val()},
success: function(data){
}
});
One comment: The use of async false is deprecated. http://api.jquery.com/jquery.ajax/
Related
I want to call a json data inside my AJAX success. I'm still new on manipulating json and AJAX. Can somebody help me on how to access my json data which is from another URL? And I want to compare the id to the JSON data. Here's my code so far:
function getCard(id){
$.ajax({
type: "GET",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# Is it possible to call another AJAX here?
}
});
}
This code will work for you
$.ajax({
url: "url",
method: "post",
data: "id=" + id,
success:function(data) {
// success goes here
$.ajax({
url: "url",
async:false,
method: "post",
data: "id=" + id,
success:function(json) {
JSON.parse(json);
// compare data and json here
},
error: function(){
// error code goes here
}
});
},
error: function(){
// error code goes here
}
});
yes Zuma you can call another Ajax inside Success function of Ajax call. Below is the example:
$.ajax({
type: "post",
url: url1,
data: data1,
success: function(data){
$.ajax({
type: "post",
url: url2,
data: data2,
success: function(data){
});
});
});
function getCard(id){
$.ajax({
type: "Get",
url: "发送请求的地址",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# if success,you can call another AJAX.
# Is it possible to call another AJAX here?
# yes!
}
});
}
I am facing some problem in assigning data to div on my view page.
The code is something like this:
$.ajax({
url: "/api/Flight/SearchFlight",
type: "Post",
data: $('form').serialize() + '&' + $.param({
'TokenId': $("#pageInitCounter").val()
}, true),
success: function(data) {
alert(data);
$('#responsestatus').text(data.Response);
$('#divResult').html(data);
});
Here, as you can see, I can see the whole data in alert box. also, I have assigned the whole data to div and i can see whole data perfectly on my page.
The Sample Response which i got back is a huge data so i am posting link for it http://pastebin.com/eEE72ySk
Now I want to iterate through each and every data but unable to do it.
Example:
$('#responsestatus').text(data.Response.ResponseStatus);
Then error is:
UncaughtTypeError:Cannot read property 'ResponseStatus' of undefined
Please Someone tell me what is wrong here. Why cant I iterate over data in response
You are getting your response back as a string, but trying to operate on it like it were a javascript object.
There is one of two things you could do
Tell the server you're expecting json data back
Parse the string response to json after it is received.
The first should be as simple as setting the datatype property on the request
$.ajax({
url: "/api/Flight/SearchFlight",
type: "Post",
datatype: 'json',
data: $('form').serialize() + '&' + $.param({
'TokenId': $("#pageInitCounter").val()
}, true),
success: function(data) {
$('#responsestatus').text(data.Response.ResponseStatus);
});
The second involves parsing the response before using it
$.ajax({
url: "/api/Flight/SearchFlight",
type: "Post",
data: $('form').serialize() + '&' + $.param({
'TokenId': $("#pageInitCounter").val()
}, true),
success: function(data) {
var result = JSON.parse(data);
$('#responsestatus').text(result.Response.ResponseStatus);
});
Use Json datatype..i wrote a sample here..
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
how to send large base64 data Array using jQuery Ajax. Here is my code :
$.ajax({
type: "POST",
url: "addPhoto.php",
data:{photosArray:photosArray},
dataType: "json",
success: function(data) {
$(data).each(function(){
...
});
}
});
photosArray contains between 3 and 12 very long strings like :
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAMAAAC6V+0...
Is there any limit for POST data size in Ajax?
Open your php.ini file and find the line stating upload_max_filesize. The default it set to 2M, which is 2MB. Try increasing it to 3MB and see if you are still receiving the error.
And use
"cache": false
Is your data properly declared ? It can be either String, object or array. try following
$.ajax({
type: "POST",
url: "addPhoto.php",
data:"{photosArray:photosArray}",
dataType: "json",
success: function(data) {
$(data).each(function(){
...
});
}
});
I have this ajax :
$.ajax({
url: '/PostComment/AddComment',
type: 'POST',
dataType: 'json',
cache: false,
data: { "PostId": PostId, "CommentText": CommentText },
success: function (data){
alert('Ok');
}
});
The problem is that when the CommentText variable contain any html tag the ajax call fail. I know this is a strange problem, but that is what happen.
Try sending encoded values to the server side:
commentText = encodeURIComponent(commentText);
On the server side, if you are using Java, then you can do:
String commentStr = URLDecoder.decode(request.getParameter("commentText"), "UTF-8");
In javascript :
JSON.stringify(CommentText));
See : 4 Things You Must Do When Putting HTML in JSON
I query ASP.NET MVC WebAPI method using jQuery. On the server I receive value with incorrect encoding when I send russian chars.
On the server URL looks like that: http://example.com:8080/api/enums?term=��
� - instead of russian characters.
Here is jQuery ajax request code:
$.ajax({
url: "/api/enums",
data: "term=" + (options.term || ""),
cache: false,
contentType: "application/json;charset=UTF-8",
success: callback,
});
I've put contentType parameter but no effect. Any thoughts?
Silly of me. Here is correct jQuery code:
$.ajax({
url: "/api/enums",
data: {
term: options.term || ""
}
cache: false,
contentType: "application/json;charset=UTF-8",
success: callback,
});