Thinkphp Learning Diary: Jquery_ajax data submission

Source: Internet
Author: User

Recently in play thinkphp, nonsense not much to say, say the matter.

Client JS Commit Code

1 $.post (' Http://localhost/app/index.php/Index/Index/handle ', {username:document.getElementById (' Username '). Value,content:document.getelementbyid (' content '). Value,},function (data) {Console.log (data); Alert (' OK ');});

The main is to submit the contents of the two form items to handle, if the successful return on the execution function functions pop up an OK

PS: This code is not the standard jQuery notation, the standard should be

$ (' input[name=username] '). Val ()

$ (' #username '). Val ()

Server-side scripting

1      Public functionhandle () {2         if(!Isajax) {3Halt (' Wrong request '));4              5}//else6 ////echo ' request success ';7 //// Var_dump (I (' post. '));8         $wi=m (' Wish ');9         $data[' Wi_name ']=i (' username '));Ten         $data[' Wi_content ']=i (' content ')); One         $data[' Wi_time ']= Time();  A         $result=$wi->data ($data),Add (); -         //Var_dump ($result); -         if($result) { the             //$this->success (' insert Success '); - //$this->ajaxreturn (' 110 ', ' insert Success ', 1); -             $dataReturn[' Status ']=1; -             $dataReturn[' Info ']= ' The post is OK '; +             $this->ajaxreturn ($dataReturn, ' JSON '); -}Else{ +Halt (' Insert failed '); A         } at  -}

However, the data submitted successfully, there has been no popup window, that is, the callback function has not been executed. Looking for a long time, I thought it was a JSON format problem, only to know that the code I commented on

//else ////echo ' request success ';////Var_dump (I (' post. '));  

ps: Today again, the discovery should be thinkphp return the echo content to the front end, and because Echo's content does not conform to the jquery format, it does not parse properly
So I replaced Echo's content with  echo" {info: ' 1213 ', Name: ' 4442 '} ";
Then, the page finally reacts, with console.log (data), after printing the content is {info: ' 1213 ', Name: ' 4442 '}, but does not recognize JSON format

OK, and found new reasons, not before, if the background with echo or other output, thinkphp will upload it to the foreground, As the return value for this Ajax request, but if you later come up with a $this->ajaxreturn ($dataReturn, ' json '), you will get an error, do not execute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.