I have this code, a simple jQuery GET:
$.get(url, params, function(){
function_call()
})
I wonder why the function_call() is never executed. The server at url is up and running and changing the function to $.ajax() shows no errors (the error option is not executed), but it's not working.
Any clue? params is a simple JS object of two fields, and of course I've used $.get() thousands of times with no problems.
Based on the information you've given (and the documentation), it should be working. Are you absolutely sure you're actually calling it (the $.get)? Can you create a minimal, self-contained example of the problem? (That does two things: 1. It usually helps you solve the problem yourself, because you figure it out in the process; 2. It gives us more to work with. :-) )
Alternately, anomareh mentioned (in a comment) the Same Origin Policy, which you might be running into.
Your syntax looks good. When something like this doesn't work for me, it's because I have a silly mistake at some other point, like a syntax error in the callback function or formulating the URL etc.
We would need to see a little more of your process to help more. Try making you're callback function only do a simple alert. Have an alert right before the $.get() is called, ideally spouting the URL. If you control the server functionality, put a breakpoint there (or whatever you need to do) to make sure the call is getting there. Switch back to $.ajax() and see if you get the error callback function called if you switch the URL to something bogus.
I guess I make so many dumb mistakes with my javascript/jquery, that I'm good with debugging ideas. :)
Have you tried a semicolon after function_call()?
The only other thing I would try is $.get(url, params, function_call) --- this will work only if your function call has no params -- take note that function_call intentionally does not have () after it.
Related
Lets take example of the following code below.
<script>
function abc()
{
$.ajax({
type: "GET",
url: "https://zx/abc/def",
timeout: 6000,
dataType: "jsonp",
error: function(h, j, e) {
console.log(h+" "+j+" "+e);
}
})
}
</script>
<button onclick="abc()">Start Call</button>
The url used above is a non-existant/invalid one. Now lets see whats the output of the above code in different browsers:
Chrome,firefox, IE 11 -> [object Object] timeout timeout
IE 8,9,10 -> [object Object] parsererror Error: jQuery111308894510177821542_1433915740650 was not called
So the question why we get different error messages?
Where do we start on this.
Do we target the answer to first time ajax programmers, or to very experienced programmers? - and what is actually being asked here?
The simple answer is obvious. The browsers are different, so they react to an error in different ways, which propagate to jquery, and finally results in two different error messages.
Since jquery wraps the very browser depending javascript engines, it will not always be able to behave in a fully crossbrowser way.
However thats really not what you are asking now is it?
You probably want to know what's going wrong..
One browser states that you have a timeout, the other that the browser is unable to parse the recieved data, and fails to call a jquery function, that is reponsible of doing so...
you may want to read this related question:
Callback - Parseerror JSONP via jQuery AJAX
The most probable cause IMHO is that the the data you are recieving from the server is not actually of the type jsonp.. or it is incomplete, or broken.
It may be text? or xml? or just plain and simple json.
try changing the type to text, and see if the error messages change.
but... why does chrome not give the same error as IE?
well, if I'm right about the data not actually being proper jsonp, it could be related to what happens when jquery "parses" json.
Jquery will use the browser built in methods (when available), a probable reason is that chrome is used to parse the data, but when something goes wrong, it times out
where can I get documentation?
So many places have bits and pieces of the documentation required to understand this. To explain this genericly for all kinds of data, and possible errors, refer to the Jquery source code, and the javascript engine documentation for I.E and Chrome respectively.
It can take a long time to read, since the behavhior is different for different versions of I.E and Chrome. In the old days we had to learn this just to make the simplest crossbrowser things work. Jquery hides most of the differences, and this is my best bet on the succes of jquery. We no longer need to know all those subtle differences. We just need to know where to look when something goes wrong..
A good place to start is here: http://api.jquery.com/jquery.parsejson/
Where the browser provides a native implementation of JSON.parse,
I'm trying to pass parameters from Flash (as 3.0) to JavaScript.
Tried all methods I found in via. Google, as:
ExternalInterface.addCallback ("fonts", recieveFromJS);
Always one and the same problem; when I try to call the fonts () swfobject, JavaScript gives the error that the method doesn't exist.
Assuming your javascript code does not have a syntax error somewhere, this usually happens because of jquery (or some other js bundle) is stepping on your code. Try using a test page with just the javascript you need, removing all other code and header entries. If it works, then add scripts and links back, one at a time, and you will find which code is breaking it. If it does not work even in your test page, then you have a code/syntax/logic problem with the snippet of code you are working with. If you still have a problem with a code snippet, post it here and I or someone will surely help debug it for you.
I'd love to live in a lint free world. JSLint that is. But I'm having some problems that I'm not sure I should ignore or fix. Specifically I'm getting a number of:
[functionName] not defined
errors. I do recognise that you should always define the function before using it and I assume that's what the message is really warning against but here are two cases where I think there is a valid exception:
jQuery(document).ready(function($) {
// code goes here
}
In a simple file which starts with a typical noconflict wrapper for jQuery I get a warning saying that "jQuery(document).ready(function($) {" is not defined. Obviously jQuery is defined in a separate file that needs to stay separate. Does that make sense?
The second example is really quite similar although arguably it is more avoidable. In this case I have two JS files in my project. One that looks just like the one above (aka, it's a set of DOM triggered events wrapped inside of jQuery's "ready" event). The other has a number of helper functions that look like this:
function doSomethingImportant() { };
function doSomethingImpressive() { };
These functions are then called within the first JS file. JSLint doesn't like this and complains every time the first JS file calls a function defined in the second JS file: doSomethingImportant() { is not defined. Yet in reality, since the functions are defined at load time and the calls to those functions always happen afterward based on DOM events there never seems to be a real problem.
Anyway, open to ideas. Is this a case of filtering JSLint's advice or something I should fix in code?
You're getting these errors because JSLint assumes that at some point in the future you might do:
var doSomethingImpressive = function() { }
in which case you would get an error, because the file where the function is defined is included after the function call (as explained here)
The same goes for the jQuery call. You can either change the order of your scripts, or safely ignore the errors.
I'd like to write a test case (using Selenium, but not the point of this question) to validate that my web application has no script errors\warnings or unhanded exceptions at certain points in time (like after initializing a major library).
This information can easily be seen in the debug consoles of most browsers. Is it possible to execute a javascript statement to get this information programatically?
It's okay if it's different for each browser, I can deal with that.
not so far read about your issue (as far as I understood your problem) here
The idea be the following:
I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors), so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I'm going to describe one of them here. Let me state again, for the record, that this is pretty hacky. I'd love to hear from others who may have better solutions.
I simply add a script to my page that will catch any JS errors by intercepting the window.onerror event:
<script type="text/javascript">
window.onerror=function(msg){
$("body").attr("JSError",msg);
}
</script>
This will cause an attribute called JSError with a value corresponding to the JavaScript error message to be added to the body tag of my document if a JavaScript error occurs. Note that I'm using jQuery to do this, so this specific example won't work if jQuery fails to load. Then, in my Selenium test, I just use the command assertElementNotPresent with a target of //body[#JSError]. Now, if any JavaScript errors occur on the page my test will fail and I'll know I have to address them first. If, for some strange reason, I want to check for a particular JavaScript error, I could use the assertElementPresent command with a target of //body[#JSError='the error message'].
Hope this fresh idea helps you :)
try {
//code
} catch(exception) {
//send ajax request: exception.message, exception.stack, etc.
}
More info - MDN Documentation
I'm trying to make a PoC of reflected Cross-Site Scripting on a website that I'm testing right now. I've found a place inside of a Javascript code where commands can be injected, however the trouble is that there the previous block of code throws a 'not defined' error and therefore (at least I think so) my injected code is not executed. Is there any chance to execute the code anyway?
Here is the code:
UndefinedObject.Init({
Var1:"a",
Var2:"b",
Var3:"can_be_injected_with_JS_code")}
I can't inject any HTML tags as these are filtered by the application.
Many thanks!
Wrap them under try catch block.
In a sequence of execution, if the code fails, the remaining part will not be executed. Javascript errors ("Exceptions") can be caught using try...catch (if you are able to inject this try - catch also).
If there is a different flow (via another event), the code will continue.
You can either try using a try-catch, or if that won't help, try using window.onerror
Generally the right way of doing that is using try-catch-finally or try-finally:
If you make something about the error - log or do something else. Catch may be also used to execute your code, but not a good practice. You can do nothing about the error if you want, that`s why finally is used.
Finally is used when it is important to execute a piece of code, no matter if an error is thrown or not. For example in C++ or other language when you work with files inside finally the file is closed ( you can not leave it opened ). Look here for some examples.