Difference betweene datatype XML and HTML - ajax

What is the difference between
$.ajax({
type: "GET",
url: "logs/old-vs-new.xml",
dataType: "xml",
success: function(xml) { ...
And
$.ajax({
type: "GET",
url: "logs/old-vs-new.xml",
dataType: "html",
success: function(xml) { ...
I know the dataType is different. But when I use datatype xml I get the parsererror and with datatype html it works fine. It's strange because my file I want to work with is a XML file.
My XML file consist now out of more than 5000 lines, but when a decrement it to about 800 lines it works too with datatype XML.
Someone an idea?

My problem is solved.
My markup wasn't right.
I used special chars between my tags and that was my fault. Now i edited my code and replaced the special chars with html entities.
So peoples with the same problem as me, first try to validate your xml code with a validator which you can find on the web.
Thanks Tony Hopkinson and florin.prisecariu!

Related

redirecting is not working in ajax

This below script is in view/template folder.
<script>
$(document).ready(function(){
$("#change").click(function(){
$.ajax({
type: "POST",
url: "common/customer.php",
data: { oldemail: $("#oldemail").val(), newemail: $("#newemail").val() }
});
});
});
</script>
I want to send data in common/customer.php but it is not working. I
already used ../common/customer.php but same problem. What is the
solution?
Its a absolute path problem.
Your script is in view/template
If your path of javascript file is like something,
view/template/myjavascript.js
And if your path of customer.php file is
view/common/
Then you must switch your directory by ../
Use
../common/customer.php
Its a relative path of your file.
According to jQuery documentation, you must declare the data type:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
TRY...
url: "/common/customer.php", // note the leading slash
Sorry friends problem was in my html code. I used the type of input submit but when i changed it into button then it is working fine. Thanks to all of you.

Ajax serialise - issue with data format

I have several inputs formatted with this jquery plugin here.
I use $.ajax to do my mysql insert:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
}),
I face an issue as my input values are formatted with the plugins and can't get into mysql db.
As an example:
Input value: $450,000.00 is not accepted.
Is there a way to unformat within the serialise function values that have a specific classes (like class="money")?
Thanks for your help!
I have tried the below code:
$.ajax({
type: 'GET',
url: 'xxx.php',
data: $('#new_form').serialize(),
dataType:"json",
beforeSend: function(){
$(".money").cleanVal();
},
<script>
function cleanVal(v) {
return v.replace(/^\,/,'');
};
</script>
the result of the insert in mysql is still 450 for 450,000.
Do you have an idea?
thanks
You can try using the plugin $.cleanVal() method to retrieve the unmasked type value of the corresponding HTML element, prior to your AJAX form submission. So something like this:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
beforeSend: function(){
$(".money").cleanVal();
}
}),
I couldn't make it work with beforehand. I found a solution which is to unmask values before calling ajax.
If anyone knows why it does not work with beforesend, thanks for letting me know.
cheers

Ajax data collect in code lines

I did some admin panel in wordpress sheet but i'm adding options and have everything in one line in the data, it's pain if I keep adding options, works that way but it looks messy.
example
$.ajax({
type: 'POST',
url: ajaxurl,
data: 'action=general_settings_action&zkr_logo='+zkrlogo+'&zkr_favicon='+zkrfavicon+'&zkr_background='+zkrbackground+'&zkr_linkcolor='+zkrlinkcolor+'&zkr_linkhover='+zkrlinkhover+'&zkr_colorbackground='+zkrcolorbackground,
success: function(data){
alert(data);
}});
I would like to make some lines to that data field like for example
$.ajax({
type: 'POST',
url: ajaxurl,
data:
'action=general_settings_action&
zkr_logo='+zkrlogo+'&
zkr_favicon='+zkrfavicon+'&
zkr_background='+zkrbackground+'&
zkr_linkcolor='+zkrlinkcolor+'&
zkr_linkhover='+zkrlinkhover+'&
zkr_colorbackground='+zkrcolorbackground,
success: function(data){
alert(data);
}});
But putting the code like that doesn't work I have tried with \n and some other stuff but still wont do the work.
I apreciate the help... Thanks
Try doing this:
Make a JSON data object that contains the parameters you want to send
var DATA = {
action:'general_settings_action',
zkr_logo:zkrlogo,
zkr_favicon:zkrfavicon,
zkr_background:zkrbackground,
zkr_linkcolor:zkrlinkcolor,
zkr_linkhover:zkrlinkhover,
zkr_colorbackground:zkrcolorbackground
}
The send the data in your AJAX request using the data field
$.ajax({
type: 'POST',
url: ajaxurl,
data: DATA,
success: function(data){
alert(data);
}});
I checked this out at: David Walsh's guide
You need to add
'zkr_logo=' + zkrlogo + '' +
instead of
zkr_logo='+zkrlogo+'&
then it will form one String

How to send json in post body with jQuery mobile

Please help me, what is wrong in this code?
$.ajax({
type: 'POST',
url: baseUrl+url,
data: {language: 'it'},
xhrFields: {
withCredentials : true
}
})
why server receives:
'language=it_IT'
Try to specify your dataType and use JSON.stringify():
$.ajax({
type: 'POST',
url: baseUrl+url,
data: JSON.stringify ({language: 'it'}),
xhrFields: {
withCredentials : true
},
contentType: "application/json",
dataType: 'json'
})
I just run through the same issue: for some reason, whenever you send language parameter using ajax it somehow automatically changes to get and all post parameters get lost. Solution: avoid using language parameter at all (or stringify yout data as #Agash Thamo suggested. That is strange for me and I would really like if somoeno could explain that a little bit better.

Special characters encoding in JSON response

I need to spend Spanish text in json response. I have tried all the possible ways but the message still shows weird characters on the UI. The message I want to show is,
Número de Seguro Social
But it shows up as,
N�mero de Seguro Social
On the Java side,
//response.setContentType("application/json");
//response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8");
OutputStream out = null;
out = response.getOutputStream();
out.write(jsonResponse.toString().getBytes());
out.close();
added meta tag in head section.
<meta http-equiv="content-type" content="text/html;charset=utf-8">
I have also set the content type in ajax call
$.ajax({
async: false,
cache: false,
type: get,
contentType: "application/json; charset=utf-8",
url: //url,
data: //data,
dataType: //dataType,
success: //callbackfn,
error: //errorfn
});
Nothing seem to work. Is there anything else I can do to get the special characters work as I intended?
I would test where the error is occurring first by sending the string:
"N\u00famero de Seguro Social"
To the browser that is showing the UTF string, just to make sure that it is capable of understanding and displaying the UTF string you're trying to display.
But the actual problem is probably in:
out.write(jsonResponse.toString().getBytes());
As getBytes will get the bytes for the default charset for the system, which may not be UTF-8. You can check this by calling Charset.defaultCharset();
I'm guessing jsonResponse is your own class for storing the response data and then converting it to JSON at the end. I'd recommend using a library from http://www.json.org/ like the Google JSON library for doing the JSON conversion, as there are lots of little gotchas like this one, that are solved problems, so long as you use a decent library to do the encoding/decoding.
$.ajax({
type: "Get",
url: your url,
data: { inputParam : JSON.stringify(inputParam)},
dataType: "json",
success: //callbackfn,
error: //error
});

Resources