1

I am trying to append extra parameters to query string for each ajax call

assuming the original url is: /customer/search?sort=Id, I want the processed url looks like

/customer/search?sort=Id&criteria=abc

this is the code

   $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        // Append the initial search criteria
        options.url += '&criteria=abc';
   });

the problem is: after one click, the url becomes: /customer/search?sort=Id&criteria=abc

after second click, the url becomes: /customer/search?sort=Id&criteria=abc&criteria=abc

it keeps adding up

It looks like jQuery ajax call is using the same options object, so I added a custom flag

  $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        // Append the initial search criteria
        if (!options.processed) {
            options.url += '&criteria=abc';
            options.processed = true;
        }
   });

the problem is: options.processed is always undefined. looks like every ajax call is using a unique options object. now I am confused :)

1 Answer 1

2

Can't you simply do

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
     //check if the new parameter has already ben added
     if(options.url.indexOf('&criteria') === -1){
        options.url += '&criteria=abc';
     }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I simplified the question, in real project, the appending part is dynamic, keep changing. so I can not do the indeoxOf check. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.