spine / spine

Lightweight MVC library for building JavaScript applications

Home Page:http://spine.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add default parameters to all ajax requests?

larryzhao opened this issue · comments

Hi, Guys,

I have a default parameter needs to be added to all ajax requests issued by Spine. I've tried $.ajaxSetup, it's not working. And I tried ajaxBeforeSend and ajaxPreFilter, but parameters in these two callbacks are already converted into string.

What's the recommended way to add a default parameter like { hello: world } to all Ajax sent by Spine? I am using Spine 1.0.6

Thanks a lot.

This isn't really a Spine question, but here is an example of how I add params using ajaxPreFilter:

    $.ajaxPrefilter (options, originalOptions, xhr) ->
      unless options.url.contains('http') or options.disableFilter
        params = {}
        params['auth_token'] = context.authToken if context.authToken?

        newData = $.param(params)
        options.data = if options.data then "#{options.data}&#{newData}" else newData
        options.processData = true

Hi, @jeremyhaile Thanks a lot for the sample.