-1

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..

Paco
  • 13
  • 1
  • 6
  • Are you validating on the front end or backend? Are you using any frameworks like express? – Jacob Feb 18 '20 at 10:52
  • JS is a client-side language (unless you use NodeJS), requesting a script from the server like that will work (in the sense that the browser receives the file from the server) but the script will not run, let alone be able to access GET params. What code exactly is in `validation.js`? Why not include the script with a ` –  Feb 18 '20 at 10:56
  • Also note that this is almost definitely an [xy problem](http://xyproblem.info/). Please tell us what you're trying to accomplish instead. –  Feb 18 '20 at 10:59
  • I am validating on the frontend, without any framework (aside from jquery). And after reading what a xy-problem is, I am afraid you are right about that also... :( – Paco Feb 18 '20 at 11:31

1 Answers1

0

Ajax is designed to make an HTTP request so that server-side code can process the request and provide an appropriate response.

jQuery's script dataType is designed to be used when the server-side code responds with a JavaScript.

You should not use it if you want to pass data to client-side JavaScript (which appears to be the case here).

(That said, I expect it is possible if you write your script so it uses document.currentScript to locate the <script> element (assuming jQuery creates one and doesn't just eval the response), then extract the src property of it and then parse the query string).

If you want to process the data with client-side JS. Don't use Ajax. Just define a function:

function validate(data) {
}

… and pass the data to it: validate({ validate: 'true' }).

And if you want to use Ajax, then write some server-side code. If you want to do it in JavaScript, then use Node.js with Express, and respond with JSON and not a script.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335