I have a validation-javascript for a form, that is called via AJAX und gives back a result. In the AJAX-Call I pass some data-parameters, but I don't know how to read these in the validation-script.
...
$.ajax({
url: validation.js",
dataType: "script",
data: { validate: 'true' },
success: function () {
...
In the validation.js I want to read the parameter validate, like I would when I was calling PHP ($_GET['validate']).
Is that possible? In the Browser-console, I can see, that the script is called with "validation.js?validate=true", but how can I access validate in the Javascript?
It is really hard to look up, because all I find is "Passing Parameters to/from PHP" and "getting Parameters with JS by url-substring-stuff"..
Thank you,
Paco
EDIT What I was trying to do: extend a working Javascript with a (client-side) validation-function, that can be extended over time, without touching the rest of the script. And I wanted to use the success-callback of the AJAX-call to react to the validation-result.
main.js
// validate Input on update
$.ajax({
url: validation.js",
dataType: "script",
data: { validation: validation },
success: function () {
var result = validate();
// validation not successful
if (!result.valid) { ... }
validation.js
function validate() {
var result = {
valid: true,
data: ''
};
if(foo) {
result.valid = false;
result.data = "There was an error: ...";
}
return result;
}
This was my first idea and it worked well, apart from the data-parameter in the AJAX-Call, which came later and which I couldn't evaluate. But I guess this still is xy-problem and there is a better and easier way with just writing a function as @quentin suggested..