PHP can't check for and get json sent data from jquery: -
when try returns "error". code in 1 file called index.php php:
if($_post){ $return="success."; echo json_encode($return); } jquery:
function save(){ $.ajax({ url: "./index.php", type: "post", data: { example:"example", example2:$(".content").text() }, datatype: "json" }).done(function(data){ $('#response').append('<p style="color:red;">'+data+'</p>'); alert("done"); }).fail(function(){ $('#response').append('<p style="color:red;">error</p>'); }); } this requested network tab , think ok post. network tab:
remote address:127.0.0.1:80 request url:http://localhost/test/test/test/index.php request method:post status code:200 ok request headersview source accept:application/json, text/javascript, */*; q=0.01 accept-encoding:gzip,deflate accept-language:en-us,en;q=0.8,bg;q=0.6 connection:keep-alive content-length:606 content-type:application/x-www-form-urlencoded; charset=utf-8 cookie:_ga=ga1.1.1569762264.1406894118 host:localhost origin:http://localhost referer:http://localhost/test/test/test/ user-agent:mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) chrome/37.0.2062.94 safari/537.36 x-requested-with:xmlhttprequest form dataview sourceview url encoded example:example example2: <?php if(true){ echo 'ds'; //das } .test { size: 5pt; } response headersview source connection:keep-alive content-length:1641 content-type:text/html date:tue, 09 sep 2014 20:03:46 gmt keep-alive:timeout=5, max=97 server:apache/2.4.9 (unix) openssl/1.0.1g mod_fcgid/2.3.9 php/5.5.11 mod_perl/2.0.8-dev perl/v5.16.3 x-powered-by:php/5.5.11 and here response tab in network tab:
"success."<!doctype html> <html> <head> <title>text</title> <script src="style/jquery.min.js"></script> <script> $(document).ready(function() { function save(){ $.ajax({ url: "./index.php", type: "post", data: { filename:"something.php", text:$(".content").text() }, datatype: "json" }).done(function(data){ $('#response').append('<p style="color:red;">'+data+'</p>'); alert("done"); }).fail(function(){ $('#response').append('<p style="color:red;">error</p>'); }); } $(".content").focus(function(){ $(window).bind('keydown', function(event) { if (event.ctrlkey || event.metakey) { switch (string.fromcharcode(event.which).tolowercase()) { case 's': event.preventdefault(); save(); break; } } }); }); }); </script> <style> .content { width: 80%; height: 80%; border: 1px solid #ccc; padding: 5px; } </style> </head> <body> <div id="response"></div> <pre><div class="content"> <?php if(true){ echo 'ds'; //das } .test { size: 5pt; } </div></pre> </body> </html> can tell me how fix problem?
if client expecting json, can't print other json. should exit script after that:
if($_post){ $return="success."; echo json_encode($return); exit(); } ?> <!doctype html> <html> ...
Comments
Post a Comment