55use Ajax \service \AjaxTransition ;
66use Ajax \service \Javascript ;
77use Ajax \service \JString ;
8+ use Ubiquity \utils \base \UString ;
89
910/**
1011 * @author jc
@@ -43,7 +44,7 @@ protected function _ajax($method,$url,$responseElement="",$parameters=[]) {
4344 $ ajaxParameters ["async " ]=($ async ?"true " :"false " );
4445
4546 if (isset ($ params )){
46- $ ajaxParameters ["data " ]=self ::_correctParams ($ params );
47+ $ ajaxParameters ["data " ]=self ::_correctParams ($ params, $ parameters );
4748 }
4849 if (isset ($ headers )){
4950 $ ajaxParameters ["headers " ]=$ headers ;
@@ -58,7 +59,7 @@ protected function _ajax($method,$url,$responseElement="",$parameters=[]) {
5859 }
5960
6061 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%} " ];
6263 foreach ($ validParameters as $ param =>$ mask ){
6364 if (isset ($ parameters [$ param ])){
6465 $ original [$ param ]=\str_replace ("%value% " , $ parameters [$ param ], $ mask );
@@ -67,7 +68,17 @@ protected function createAjaxParameters(&$original,$parameters){
6768 }
6869
6970 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+ }
7182 return $ s ;
7283 }
7384
@@ -151,12 +162,16 @@ protected function _correctAjaxUrl($url) {
151162 return $ url ;
152163 }
153164
154- public static function _correctParams ($ params ){
165+ public static function _correctParams ($ params, $ ajaxParameters =[] ){
155166 if (JString::isNull ($ params )){
156167 return "" ;
157168 }
158169 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+ }
160175 }
161176 return $ params ;
162177 }
0 commit comments