Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初始值 传递额外数据 #90

Open
yiqing-95 opened this issue May 2, 2019 · 2 comments
Open

初始值 传递额外数据 #90

yiqing-95 opened this issue May 2, 2019 · 2 comments
Labels

Comments

@yiqing-95
Copy link

yiqing-95 commented May 2, 2019

对于post 请求 一般会附加 csrftoken 如果后台需要这个值 在初始值ajax请求时 需要客户端传递这个数据

// SelectPage.prototype.setInitRecord

if (typeof p.data === 'object') {
				var data = new Array();
				var keyarr = key.split(',');
				$.each(keyarr,function(index,row){
					for (var i = 0; i < p.data.length; i++) {
						if (p.data[i][p.keyField] == row) {
							data.push(p.data[i]);
							break;
						}
					}
				});
				if(!p.multiple && data.length > 1) data = [data[0]];
				self.afterInit(self, data);
			} else {//ajax data source mode to init selected item
				$.ajax({
					dataType: 'json',
                    type: 'POST',
					url: p.data,
                                      //  这里 需要传递额外数据
					data: {
						searchTable: p.dbTable,
						searchKey: p.keyField,
						searchValue: key
					},
					success: function(json) {
                        var d = null;
                        if(p.eAjaxSuccess && $.isFunction(p.eAjaxSuccess)) d = p.eAjaxSuccess(json);
                        self.afterInit(self, d.list);
					},
					error: function() {
						self.ajaxErrorNotify(self);
					}
				});
			}

在data 处需要暴露接口来传递csrfToken
注意到了 在 SelectPage.prototype.searchForDb 里面 params参数这种形式可以返回额外数据

但在setInitRecord 方法实现中 没有使用 params参数的值 这样想传递额外数据 可能就不方便了

@TerryZ TerryZ added the suggest label May 2, 2019
@yiqing-95
Copy link
Author

当然 通过一些看起来比较“脏”的补丁方法 也可以实现 只不过不是那么“优雅”:

  // 添加额外的 csrf token值
        $.ajaxSetup({
            beforeSend: function(jqXHR, settings) {
                var csrfParam = $('meta[name=csrf-param]').attr('content') ;
                var csrfToken =  $('meta[name=csrf-token]').attr('content') ;
                // this.data += '&' + $.param({
                //     key: 'value'
                // });
                this.data += '&'+csrfParam+'='+csrfToken
            }
        });

目前先用这种方法搞了 不然就要去改源码添加这个口子了

@yiqing-95
Copy link
Author

往前翻了下 issues 发现前面有人已经提过了 #84

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants