jQuery ajax request succeeding in console but callback isn't called











up vote
0
down vote

favorite












I have a jQuery POST request that looks something like this:



$.ajax("/myurl",{
data:{
...
},
mimeType:"application/json",
dataType:"application/json",
method:"POST",
success:function(r){
console.log("success");
},
error:function(r){
console.log("error");
console.log(r);
}
});


I turned on XHR logging in my chrome console, and it says the XHR finished loading: POST http://localhost/myurl. However, "error" is logged in my console as well, along with an XHR object:



error
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
XHR finished loading: POST "http://localhost/myurl"


Why is it saying that the XHR finished loading, but at the same time there's an error? Why is there even an error? On the server-side, the server receives the request and sends a response.










share|improve this question






















  • The error callback receives three arguments. What are the values of the other two?
    – T.J. Crowder
    Nov 10 at 10:45















up vote
0
down vote

favorite












I have a jQuery POST request that looks something like this:



$.ajax("/myurl",{
data:{
...
},
mimeType:"application/json",
dataType:"application/json",
method:"POST",
success:function(r){
console.log("success");
},
error:function(r){
console.log("error");
console.log(r);
}
});


I turned on XHR logging in my chrome console, and it says the XHR finished loading: POST http://localhost/myurl. However, "error" is logged in my console as well, along with an XHR object:



error
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
XHR finished loading: POST "http://localhost/myurl"


Why is it saying that the XHR finished loading, but at the same time there's an error? Why is there even an error? On the server-side, the server receives the request and sends a response.










share|improve this question






















  • The error callback receives three arguments. What are the values of the other two?
    – T.J. Crowder
    Nov 10 at 10:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a jQuery POST request that looks something like this:



$.ajax("/myurl",{
data:{
...
},
mimeType:"application/json",
dataType:"application/json",
method:"POST",
success:function(r){
console.log("success");
},
error:function(r){
console.log("error");
console.log(r);
}
});


I turned on XHR logging in my chrome console, and it says the XHR finished loading: POST http://localhost/myurl. However, "error" is logged in my console as well, along with an XHR object:



error
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
XHR finished loading: POST "http://localhost/myurl"


Why is it saying that the XHR finished loading, but at the same time there's an error? Why is there even an error? On the server-side, the server receives the request and sends a response.










share|improve this question













I have a jQuery POST request that looks something like this:



$.ajax("/myurl",{
data:{
...
},
mimeType:"application/json",
dataType:"application/json",
method:"POST",
success:function(r){
console.log("success");
},
error:function(r){
console.log("error");
console.log(r);
}
});


I turned on XHR logging in my chrome console, and it says the XHR finished loading: POST http://localhost/myurl. However, "error" is logged in my console as well, along with an XHR object:



error
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
XHR finished loading: POST "http://localhost/myurl"


Why is it saying that the XHR finished loading, but at the same time there's an error? Why is there even an error? On the server-side, the server receives the request and sends a response.







javascript jquery ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 10:40









FireCubez

1228




1228












  • The error callback receives three arguments. What are the values of the other two?
    – T.J. Crowder
    Nov 10 at 10:45


















  • The error callback receives three arguments. What are the values of the other two?
    – T.J. Crowder
    Nov 10 at 10:45
















The error callback receives three arguments. What are the values of the other two?
– T.J. Crowder
Nov 10 at 10:45




The error callback receives three arguments. What are the values of the other two?
– T.J. Crowder
Nov 10 at 10:45












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The most likely explanation is that what the server is sending back isn't valid JSON, so although the ajax succeeds, parsing the JSON fails.



Your dataType is off, though, it should have one of these values: "xml", "json", "script", or "html" — in your case, "json". You also don't need (and probably don't want) to set mimeType.






share|improve this answer























  • I recall that removing mimeType actually made my code not work. I'll try though
    – FireCubez
    Nov 10 at 10:50






  • 1




    @FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
    – T.J. Crowder
    Nov 10 at 10:51










  • It is sending back application/json. Your answer solved it, i'll accept once I can
    – FireCubez
    Nov 10 at 10:52










  • @FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
    – T.J. Crowder
    Nov 10 at 10:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238119%2fjquery-ajax-request-succeeding-in-console-but-callback-isnt-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The most likely explanation is that what the server is sending back isn't valid JSON, so although the ajax succeeds, parsing the JSON fails.



Your dataType is off, though, it should have one of these values: "xml", "json", "script", or "html" — in your case, "json". You also don't need (and probably don't want) to set mimeType.






share|improve this answer























  • I recall that removing mimeType actually made my code not work. I'll try though
    – FireCubez
    Nov 10 at 10:50






  • 1




    @FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
    – T.J. Crowder
    Nov 10 at 10:51










  • It is sending back application/json. Your answer solved it, i'll accept once I can
    – FireCubez
    Nov 10 at 10:52










  • @FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
    – T.J. Crowder
    Nov 10 at 10:54















up vote
1
down vote



accepted










The most likely explanation is that what the server is sending back isn't valid JSON, so although the ajax succeeds, parsing the JSON fails.



Your dataType is off, though, it should have one of these values: "xml", "json", "script", or "html" — in your case, "json". You also don't need (and probably don't want) to set mimeType.






share|improve this answer























  • I recall that removing mimeType actually made my code not work. I'll try though
    – FireCubez
    Nov 10 at 10:50






  • 1




    @FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
    – T.J. Crowder
    Nov 10 at 10:51










  • It is sending back application/json. Your answer solved it, i'll accept once I can
    – FireCubez
    Nov 10 at 10:52










  • @FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
    – T.J. Crowder
    Nov 10 at 10:54













up vote
1
down vote



accepted







up vote
1
down vote



accepted






The most likely explanation is that what the server is sending back isn't valid JSON, so although the ajax succeeds, parsing the JSON fails.



Your dataType is off, though, it should have one of these values: "xml", "json", "script", or "html" — in your case, "json". You also don't need (and probably don't want) to set mimeType.






share|improve this answer














The most likely explanation is that what the server is sending back isn't valid JSON, so although the ajax succeeds, parsing the JSON fails.



Your dataType is off, though, it should have one of these values: "xml", "json", "script", or "html" — in your case, "json". You also don't need (and probably don't want) to set mimeType.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 10:50

























answered Nov 10 at 10:48









T.J. Crowder

673k11811901286




673k11811901286












  • I recall that removing mimeType actually made my code not work. I'll try though
    – FireCubez
    Nov 10 at 10:50






  • 1




    @FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
    – T.J. Crowder
    Nov 10 at 10:51










  • It is sending back application/json. Your answer solved it, i'll accept once I can
    – FireCubez
    Nov 10 at 10:52










  • @FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
    – T.J. Crowder
    Nov 10 at 10:54


















  • I recall that removing mimeType actually made my code not work. I'll try though
    – FireCubez
    Nov 10 at 10:50






  • 1




    @FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
    – T.J. Crowder
    Nov 10 at 10:51










  • It is sending back application/json. Your answer solved it, i'll accept once I can
    – FireCubez
    Nov 10 at 10:52










  • @FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
    – T.J. Crowder
    Nov 10 at 10:54
















I recall that removing mimeType actually made my code not work. I'll try though
– FireCubez
Nov 10 at 10:50




I recall that removing mimeType actually made my code not work. I'll try though
– FireCubez
Nov 10 at 10:50




1




1




@FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
– T.J. Crowder
Nov 10 at 10:51




@FireCubez - That's because your dataType is incorrect. If you fix it, and remove mimeType, the response will be treated as JSON. (All of this is a workaround, though; instead, the server should be sending back the correct Content-Type header: application/json.)
– T.J. Crowder
Nov 10 at 10:51












It is sending back application/json. Your answer solved it, i'll accept once I can
– FireCubez
Nov 10 at 10:52




It is sending back application/json. Your answer solved it, i'll accept once I can
– FireCubez
Nov 10 at 10:52












@FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
– T.J. Crowder
Nov 10 at 10:54




@FireCubez - Good deal! Then you can remove both mimeType and dataType. Happy coding!
– T.J. Crowder
Nov 10 at 10:54


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238119%2fjquery-ajax-request-succeeding-in-console-but-callback-isnt-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff