In my .cshtml page I have JavaScript function it is working fine but showing error like Invalid Character. Here is my code snap :
Please help me. Not getting where is the error.
Please use like this:
.
Please click here for details documentation on Jquery.com
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
Related
I tried this code to pull website but when I test it, the alert box doesn't run.
<script type="text/javascript" >
$(document).ready(function(){
$.get( "https://crossorigin.me/https://google.com", function( data ) {
alert( "Data Loaded: " + data );
});
});
</script>
Taken from the documentation, try implementing the .fail method to see what went wrong.
var jqxhr = $.get( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
Also the author of crossorigin.com warns that
PLEASE DO NOT USE THE PROXY ON A PRODUCTION SITE.
I cannot guarantee the stability of the service, and you will probably experience service interruptions.
I am new to javascript and jquery, in my code I'm initiating a AJAX call and I get the following response.
I'm trying to implement an autocomplete functionality. I am using below code to do AJAX call.
$( "#city" ).autocomplete({
source: function( request, response ) {
jQuery.ajax({
url: "the url",
data: {SearchTerm: request.term}
success: function (data) {
console.log("the data is" +data);
response(data);
}
}).fail(function (jqXHR, textStatus, error) {
console.log("failure1" + textStatus);
console.log("failure2" + jqXHR.status);
});
},
minLength: 3,
select: function( event, ui ) {
console.log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
Even though I see 200 response from the server and the response in JSON format, success method doesn't get called and fail get called. Since I'm getting JSON response with 200 status, doesn't success method should get called?
Try this, may be this will help you :
Option 1 :
May be you accidently turning On Magic Quotes in PHP
But this function is already deprecated in PHP 5.3.0.
And REMOVED as of PHP 5.4.0. as mentioned in here
Go to your php.ini file
Search this keyword : magic_quotes_gpc
Set it to OFF : magic_quotes_gpc = Off
Option 2 :
Do it on your code like this :
if (get_magic_quotes_gpc()) {
$returnedValue = stripslashes($yourReturnedValue);
}
I am trying to get jquery to read json data but getting undefined.
$.post( wce_data.url, { userid : userId, token : accessToken }, 'json')
.done(function( data ) {
console.log("data.status: " + data.status);
if (data.status === "success") {
alert("Coupon added");
} else {
alert("failed");
}
})
.fail(function() {
alert("The requested action failed. Please contact the site administrator!");
});
data.status: returns 'undefined'
data: returns {"status":"success"}
Try error checking like this:
.done(function() {
alert( "Coupon added" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
Oh I see. I got it. It was the PHP Output Header that was needed.
I added:
header("Content-type: application/json");
To the PHP for jQuery.
Hope this helped someone new to json like me.
If the post method is unsuccessful I want to throw an exception and write to an error log? Basically if this method returns no results I want to throw an exception and write to file.
Post method:
$.post("ip.php", function( data ){
$("#ip").val(data.ip);
$("#country").val(data.country);
$('#campaignID').val(data.campaignID);
}, "json");
Just add the fail(); method.
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
For the part of the question:
If the post method is unsuccessful I want to throw an exception (...)
You could chain a .fail() callback method to the $.post() request as mentioned by #Grimbode
Or use $.ajax() instead, where is an error option - function to be called if the request fails.
The $.ajax() equivalent to your $.post() method would be:
$.ajax({
type : "POST",
url : "ip.php",
dataType : 'json',
success : function(data){
$("#ip").val(data.ip);
$("#country").val(data.country);
$('#campaignID').val(data.campaignID);
},
error : function(jqXHR/*object*/, textStatus/*string*/, errorThrown/*object*/){
// throw an error ...
// console.log('Type of error: ' + textStatus);
},
});
As for that part:
(...) write to an error log
It's not possible to write any file with javascript. Since it's a client side scripting language that executes in a client browser, it has no access to the server. Well, that's how it works.
I have a $.post which using Fiddler I can see always posts successfully but my success function is never fired.
A successful post to the url returns a 1 or failure returns a 0, on each occasion I have tested it returns the expected value of 1 so I'm at a lost as to what I'm doing wrong?
if ($('#mycheckbox').prop('checked')) {
$.post('/base/MailingList/Subscribe/',
{
listId: $('#mycheckbox').val(),
name: $('#subscriber-name').val(),
email: $("#subscriber-email").val()
},
function(response) {
console.log(response);
});
}
Stick another function as a final callback. That function will run in the failure case.
The way jquery recommends doing it is this:
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
I'm not sure if this will help or not, but I always use fully qualified urls when I do any asynchronous calls. You may want to try:
$.post('http://mydomain.com/base/MailingList/Suscribe/', ...
Additionally, you will likely want your handling function to recieve all three argument from the callback (data, textStatus, jqXHR). This way, you can look at the txtStatus to verify if you have success or not.
In my case the problem was header with wrong MIME type ("application/json" instead of "text") in php file on server. If you send header that doesn't match type of data cause an error and $.post calls fail function.