I have the 'unexpected character error' problem, my Jquery ajax code looks like that:
function test(){
if(true){
$.ajax({
type: 'POST',
url: 'test.php',
dataType: 'json',
data: {
godot: 'godot',
jobadze: 'jobadze'
},
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) { alert("Error Status: "+textStatus+"\nMessage: "+errorThrown);
}
});
and this is the php code:
<?php
echo 'test';
?>
it should alert "test", but it calls error. What is going on?
You're not returning any JSON. You returning text but you've specified in the AJAX that it will return json.
You have: dataType: 'json',
You could change the dataType: 'text', if you will always be returning text
or in your php change echo 'test'; to echo json_encode('test');
Hope this helps
You wrote dataType: 'json', so the PHP script is required to return valid JSON. Since you're not, the it gets an error when it tries to parse the response as JSON, and reports that error.
You should use json_encode:
<?php
echo json_encode('test');
?>
it should alert "test", but it calls error. What is going on?
Reason for this is your dataType : "json" in $.ajax() method which expects the response from serverside should be a json, which is not the case because that is just a simple text string nothing else, so what could you do:
Either remove the dataType or change the dataType: "text"
Or do a json_encode('string') at your serverside.
As you asked in your question
It should alert "test",
so you can skip the #2 and do this:
$.ajax({
type: 'POST',
url: 'test.php',
dataType: 'text',
data: {
godot: 'godot',
jobadze: 'jobadze'
},
success: function(data){
alert(data); // will alert "test".
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error Status: "+textStatus+"\nMessage: "+errorThrown);
}
});
but it calls error
$.ajax({
type: 'POST',
url: 'test.php',
dataType: 'json', //<----because of this
See json is a {key : value} pair js object and from your php you are just echoing a string not a object.
Related
How do I print the success variable r in script tag. The r variable contains the value from api. But I am getting php errors.
if (Login::isLoggedIn()) {
echo "
$('.btn1').click(function(){
$.ajax({
type: 'POST',
url: 'api/follow?id={$userid}&artid={$id}',
processData: false,
contentType: 'json/application',
data: '',
success: function(r){
console.log(r)
('#followdiv').html("'+r.follow+'")//error
},
error: function(r){
}
});
});
";
}
how do I print r.follow?
I tried in different ways but its not working. Plz help me.
You don't need to concatenate r.follow with anything, so get rid of "'+r.follow+'".
Your contentType: 'json/application' is wrong in two ways: first, the correct name is application/json; second, you're sending empty data, that's not valid JSON. You're sending all the parameters in the URL, it's not clear why you're using POST at all.
If you meant to specify that the response is JSON, that's done using the dataType: option, not contentType:.
echo "
$('.btn1').click(function(){
$.ajax({
type: 'POST',
url: 'api/follow?id={$userid}&artid={$id}',
processData: false,
dataType: 'json',
data: '',
success: function(r){
console.log(r)
$('#followdiv').html(r.follow)
},
error: function(r){
}
});
});
";
I Am trying to send value from ajax to php and retrieve it just to test that everything is work, when i click in a button to test i got error and alert('Failed') Appears , how can i fix it in order to get success? thanks
Ajax :
var a = "test";
$.ajax({
url: "search.php",
dataType: "json",
data: a ,
success: function(data) {
alert('Successfully');
},
error: function(data) {
alert('Failed');
}
})
PHP :
<?php
$pictures = "img1";
echo json_encode($pictures);
?>
I refined your code slightly and it works.
var a = "test";
$.ajax({
type: 'POST',
url: 'search.php',
data: 'a=' + a,
dataType: 'json',
cache: false,
success: function (result) {
alert('Successful');
},
error: function (result) {
alert('Failed');
}
});
If you're requesting a JSON, use the $.getJSON from jQuery, it's aready parse the JSON into a JSON object for you.
Seems that you're not return an actual JSON from server, maybe this is what is causing the error.
If you're seeing the 'Failed' message probably the problem is a 500 error which is a server error.
Try this code above.
Javascript:
var a = "test";
$.getJSON("search.php", {
a: a
}, function (json) {
console.log(json);
});
PHP:
<?php
$pictures = ["img1"];
echo json_encode($pictures);
The only way to this not work, is if you have a huge mistake on you webserver configuration.
Your ajax is wrong, it should be:
var a = "test";
$.ajax({
type: "POST",
url: "search.php",
dataType: "json",
data: {a:a},
success: function(data) {
alert('Successfully');
},
error: function(data) {
alert('Failed');
}
});
I have the following Jquery code that sends a ajax request to add-to-cart.php.
var productData = {
"product_id": s_product_id,
"product_opties": s_product_opties,
"product_aantal": s_product_aantal
}
productData = JSON.stringify(productData);
$.ajax({
url: 'inc/add-to-cart.php',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: productData,
type: 'POST',
success: function(response) {
alert(response.d);
},
error: function(e){
console.log(e);
}
});
Add-to-cart.php:
<?php
print $_POST['product_id'];
?>
I am having two problems, the first one is that the $_POST['product_id'] does not exists when i ask for it and secondly when the ajax response returns it goes directly to the error: function and not succes function
The console log says:
[object Object]{readyState: 4, responseText: "<br /> <b>N...", status: 200, statusText: "OK"}
If the status is OK why does it jump to the error: part?
Thanks!
Try with:
...
var productData = {
'product_id': s_product_id,
'product_opties': product_opties,
'product_aantal': product_aantal,
}
$.ajax({
url: 'inc/add-to-cart.php',
dataType: 'json',
data: productData,
type: 'POST',
success: function(response) {
console.log(response.d);
},
error: function(e){
console.log(e);
}
});
...
Omitting the AJAX contentType parameter and the part where you stringify your JSON, that's already ready to be sent to your url, so, isn't needed.
Remove the line
productData = JSON.stringify(productData);
Then do not return HTML <br> ... from add_to_cart.php, you need to return a JSON string created by the PHP function json_encode and NOTHING ELSE.
Like in:
<?php echo json_encode(array('d' => 'value of d'));
First: status Code from the Webserver is 200 cause he deliverd an existing site.
Second: You dont need to stringify the json object by urself
Here is My ajax call
$.ajax({
url:'http://localhost:8081/organizations/',
data: JSON.stringify({"name":"karthik"}),
type: 'POST',
dataType :'json',
processData : true,
success: (function(response){ alert("response "+response);}),
error: (function(err){ console.log(err); })
});
In the api, when i tried to print request.data, why i am getting like this {u'{"name":"karthik"}': [u'']}
Replace below line
success: (function(response){ alert("response "+response);}),
by newone
success: (function(response){ alert(JSON.stringify(response));}),
In your code and then try it will work.
I am just trying to read the file content of a rendering HTML of URL
Here the code i am using , its always going in error section .
$.ajax({
type: 'POST',
url: 'http://www.withholding32.com/api/wh32calc.php?userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0',
dataType: 'html',
success: function(data) {
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
but in case i run the same url directly in browser , its show me html.
here is url
Working DEMO
You can use this in your head tag
<script src="https://rawgithub.com/IonicaBizau/jQuery-cross-domain-requests/master/js/jquery.xdomainajax.js">
</script>
code
$.ajax({
url: 'http://www.withholding32.com/api/wh32calc.php?userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0', // Or your web page link
type: 'GET',
success: function(res) {
var headline = res.responseText;
$('body').append(headline);
}
});
Hope this helps, Thank you
Try the below code:
$('document').ready(function() {
$.getJSON('http://anyorigin.com/get?url=' +
encodeURIComponent('http://www.withholding32.com/api/wh32calc.php?userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0') + '&callback=?',
function(data){
$("#result").html(data.contents);
});
});
Refer : http://jsfiddle.net/R7EPt/275/
Change your request type to GET, all your parameters are given in the URL.
if you are using post method for the ajax than you can't pass argument with url and also add control origin to your php file.
try this...
AJAX code:
$.ajax({
type: 'POST',
url: 'http://www.withholding32.com/api/wh32calc.php',
dataType: 'html',
async:false,
data: 'userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0',
success: function(data) {
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
PHP CODE:
header("Access-Control-Allow-Origin: *");