5
5
use Ajax \service \AjaxTransition ;
6
6
use Ajax \service \Javascript ;
7
7
use Ajax \service \JString ;
8
+ use Ubiquity \utils \base \UString ;
8
9
9
10
/**
10
11
* @author jc
@@ -43,7 +44,7 @@ protected function _ajax($method,$url,$responseElement="",$parameters=[]) {
43
44
$ ajaxParameters ["async " ]=($ async ?"true " :"false " );
44
45
45
46
if (isset ($ params )){
46
- $ ajaxParameters ["data " ]=self ::_correctParams ($ params );
47
+ $ ajaxParameters ["data " ]=self ::_correctParams ($ params, $ parameters );
47
48
}
48
49
if (isset ($ headers )){
49
50
$ ajaxParameters ["headers " ]=$ headers ;
@@ -58,7 +59,7 @@ protected function _ajax($method,$url,$responseElement="",$parameters=[]) {
58
59
}
59
60
60
61
protected function createAjaxParameters (&$ original ,$ parameters ){
61
- $ validParameters =["dataType " =>"'%value%' " ,"beforeSend " =>"function(jqXHR,settings){%value%} " ,"complete " =>"function(jqXHR){%value%} " ];
62
+ $ validParameters =["contentType " => " %value% " , " dataType " =>"'%value%' " ,"beforeSend " =>"function(jqXHR,settings){%value%} " ,"complete " =>"function(jqXHR){%value%} " ];
62
63
foreach ($ validParameters as $ param =>$ mask ){
63
64
if (isset ($ parameters [$ param ])){
64
65
$ original [$ param ]=\str_replace ("%value% " , $ parameters [$ param ], $ mask );
@@ -67,7 +68,17 @@ protected function createAjaxParameters(&$original,$parameters){
67
68
}
68
69
69
70
protected function implodeAjaxParameters ($ ajaxParameters ){
70
- $ s = '' ; foreach ($ ajaxParameters as $ k =>$ v ) { if ($ s !== '' ) { $ s .= ', ' ; } $ s .= "' {$ k }': {$ v }" ; }
71
+ $ s = '' ;
72
+ foreach ($ ajaxParameters as $ k =>$ v ) {
73
+ if ($ s !== '' ) {
74
+ $ s .= ', ' ;
75
+ }
76
+ if (is_array ($ v )){
77
+ $ s .= "' {$ k }':{ " .self ::implodeAjaxParameters ($ v )."} " ;
78
+ }else {
79
+ $ s .= "' {$ k }': {$ v }" ;
80
+ }
81
+ }
71
82
return $ s ;
72
83
}
73
84
@@ -151,12 +162,16 @@ protected function _correctAjaxUrl($url) {
151
162
return $ url ;
152
163
}
153
164
154
- public static function _correctParams ($ params ){
165
+ public static function _correctParams ($ params, $ ajaxParameters =[] ){
155
166
if (JString::isNull ($ params )){
156
167
return "" ;
157
168
}
158
169
if (\preg_match ("@^\{.*?\}$@ " , $ params )){
159
- return '$.param( ' .$ params .') ' ;
170
+ if ( !isset ($ ajaxParameters ['contentType ' ])|| !UString::contains ('json ' , $ ajaxParameters ['contentType ' ])){
171
+ return '$.param( ' .$ params .') ' ;
172
+ }else {
173
+ return 'JSON.stringify( ' .$ params .') ' ;
174
+ }
160
175
}
161
176
return $ params ;
162
177
}
0 commit comments