I keep on getting this error on chromium but not on Firefox n its driving me nuts because I cant find a solution despite searching for hours now. I basically get JSON from the server and then wanna insert it into DOM. Here's my code...
function lookup(inputString){
if(inputString.length == 0){ //hide suggestions
$('#suggestions').empty()
.fadeOut();
}
else{ //make an AJAX call
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results){
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
})
}
}
You need a ; at the end of this:
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results){
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
});
same conclusion :)
you missed a semicolon ;
jslint could help you to find this kind of errors : http://www.jslint.com/
function lookup(inputString) {
if (inputString.length === 0) {
$('#suggestions').empty().fadeOut();
}
else {
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results) {
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
});
}
}
Related
I have a simple set up.
Jquery:
$.ajax({
url: "/MyApp/MyHandler.ashx/MyMethod",
success: function(result) {
alert("sucess");
},
error: function() {
alert('Error');
}
});
and web method:
[System.Web.Services.WebMethod]
public static void MyMethod(){
new AnotherClass(null).AnotherMethod(null, null);
}
problem is success alert is called but break point is not hit inside MyMethod.
I had the same issue and this is what I ended up having to do:
$.ajax({
url: _url,
data: '',
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: function(result) {
alert("sucess");
},
error: function() {
alert('Error');
}
});
My first attempt left out the data, dataType and contentType; Only when I set contentType: 'application/json' with an empty string (data: '') did it end up working. What a headache - hope this helps someone else!
In my case, the issue was in RoutingConfig.So, sort that in App_Start folder, within RouteConfig,commented out the following line
//settings.AutoRedirectMode = RedirectMode.Permanent;
if( $.isArray(certTypeReq) ){
$.each(certTypeReq,function(key,certType){
$.ajax({
url: baseUrl+"/user/certificate/getlink",
type: 'GET',
data: { 'certType' : certType},
dataType : 'json',
cache: false,
async:false,
success: function(data) {
window.location.href = data.link;
}
});
});
}
This is my code and its working fine in Firefox but not in chrome browser. If anyone has solution please help me.
var i=1;
if( $.isArray(certTypeReq) ){
$(certTypeReq).each(function(key,certType){
setTimeout(function () {
$.ajax({
url: baseUrl+"/user/certificate/getlink",
type: 'POST',
data: { 'certType' : certType},
dataType : 'json',
success: function(data) {
window.location.href = data.link;
}
});
}, 3000*i);
i++;
});
}
Applied delay in each iteration and it worked.
Thanks to all for your replies.
In the success function I want to call a function. The problem is that ajax does not fire, so the data is never triggered and display. Here is my ajax call with a javascript function call in the success function.
$.ajax({
type: "POST",
url: "./api/login.php",
data: dataString,
cache: false,
success: function(data){
if(data){
//FUNCTION CALL WHEN USER LOGGING IN
retrieveUserBlogData();
window.location = "api/home.php";
}else{
$('.alert').show();
}
}
});
function retrieveUserBlogData(){
$.ajax({
type: "GET",
url: 'retrievePostData.php',
data: "",
dataType: 'json',
success: handleData
});
}
function handleData(data) {
alert(data);
var blog_file = data[3];
$('#imageDiv')
.append('<img id="blog_img" src="upload/' + blog_file + '"><br>');
}
I cant figure out why the ajax in the retrieveUserBlogData() function is not being triggered.
Any help would be appreciated Thanks.
Even if the AJAX succeeds, you are redirecting the browser to a different page after the first AJAX request:
window.location = "api/home.php";
So I would suggest removing that.
Try the following code for redirecting to another window
window.location.assign(URL);
then it may work.
Try it like this
$.ajax({
type: "POST",
url: "./api/login.php",
data: dataString,
cache: false,
success: function(data){
if(data){
//FUNCTION CALL WHEN USER LOGGING IN
retrieveUserBlogData();
}else{
$('.alert').show();
}
}
});
function retrieveUserBlogData(){
$.ajax({
type: "GET",
url: 'retrievePostData.php',
data: "",
dataType: 'json',
success: function(data){
alert(data);
var blog_file = data[3];
$('#imageDiv')
.append('<img id="blog_img" src="upload/' + blog_file + '"><br>');
window.location = "api/home.php";
}
});
}
I use this trivial code, but it doesn't work correctly (for my mind):
$(document).on("click", "#summary_and_tables #tables ul li a", function() {
var url = "index.php/table/show/"+this.hash;
console.log(url);
$.ajax({
type: "POST",
url: url,
dataType: 'json',
success: function(response) {
if(response.status == 'ok') {}
}
});
});
Output in console is:
index.php/table/show/#summary
But ajax request sent to:
http://test.loc/st_base/index.php/table/show/
Unfortunately/luckily, nothing is wrong. Things work as expected. Browsers just do not send hash to the server.
If you really need to pass it, put it into data:
var url = "index.php/table/show/";
var hashOnly = this.hash;
$.ajax({
type: "POST",
data: {myhash: hashOnly},
url: url,
dataType: 'json',
success: function(response)
{
if(response.status == 'ok')
{
}
}
});
You are using relative url, this will append index.php/table/show/"+this.hash to current location of your script.
It appears that your script is located at "http://test.loc/st_base/"
Try specifying full url, starting with "http://mycorrectsite.com/blah/index.php/table/show..."
Java script
$('#senurl').click(function () {
$.ajax({
type: "POST",
url: "/Admin/Coupon1/Reject",
dataType: "json",
data: "id="+#Model.id+"&url="+#url
});
});
ReferenceError: Expired is not defined
[Break On This Error]
data: "id="+2925+"&url="+Expired
You probably want (but see also below):
$('#senurl').click(function () {
$.ajax({
type: "POST",
url: "/Admin/Coupon1/Reject",
dataType: "json",
data: "id=#Model.id&url=#url"
});
});
...because you have to think about what the browser sees, and if #url gets replaced with Expired by the server, from the error you can tell that what the browser sees for your code is:
data: "id="+2925+"&url="+Expired // <=== What the browser sees with your current code
Even better, let jQuery handle any potential URI-encoding needed by passing it an object instead:
$('#senurl').click(function () {
$.ajax({
type: "POST",
url: "/Admin/Coupon1/Reject",
dataType: "json",
data: {id: #Model.id, url: "#url"}
});
});
If you don't want to pass jQuery an object and let it handle the URI-encoding for you, then you'll want to handle it yourself:
data: "id=#Model.id&url=" + encodeURIComponent("#url")
$('#senurl').click(function () {
$.ajax({
type: "POST",
url: "/Admin/Coupon1/Reject",
dataType: "json",
data: "{id:'" + #Model.id + "', 'url': " + #url + "}",
success: function (response) {
alert( response.d);
},
error: function (data) {
alert(data);
},
failure: function (msg) {
}
});
});
Try this it is working fine. If you are using url routing then you might get some other error.
so better get the respone output and check..
I think its because #url variable is assigned data Expired without quotes.