简体   繁体   English

通过 getJSON 将 HTML 作为 javascript 变量发送不起作用

[英]Sending HTML as javascript variable through getJSON doesn't work

I've seen some questions about sending HTML data from PHP to an HTML page, but this question is actually for the contrary (I can get HTML from PHP fine with json_encode). I've seen some questions about sending HTML data from PHP to an HTML page, but this question is actually for the contrary (I can get HTML from PHP fine with json_encode).

A small snippet of code that explains most of it:一小段代码解释了大部分内容:

savePage = function() {
 var pagecontents = $("#editcontents").val();
 log("Attempting to save page...");
 $.getJSON(docTools+"?jsoncallback=?",{action:'update', loginCookie:loginCookie, page:thisFile, newdata:pagecontents}, function(data) {
  if(data.response[0].answer === 'true') {
   log('Page'+thisFile+' was saved correctly: '+data.response[0].details);
   $("#editcontents, #saveedit, #canceledit").remove();
   $("#bodycontents").html(pagecontents);
  } else {
   log('Failed to save page. Error (' + data.response[0].errorcode + ') : ' + data.response[0].errormessage);
  }
 });
}

FYI: log just does console.log if it exists.仅供参考:如果存在,log 只会执行 console.log。

So basically what happens here is that the pagecontents variable contains HTML data, all within a <body> tag (meaning no header information if that matters).所以基本上这里发生的是pagecontents变量包含 HTML 数据,都在<body>标记内(如果重要的话,意味着没有 header 信息)。 However, when I actually call this function, the log() function works fine... but the getJSON() never completes (actually, as far as I can tell it never makes it to my PHP server).但是,当我实际调用此 function 时,log() function 工作正常……但 getJSON() 永远不会完成(实际上,据我所知,它永远不会到达我的 Z2FEC392324A5C23AC138D 服务器)。

If I replace pagecontents with just a standard string, it works fine.如果我只用标准字符串替换pagecontents ,它就可以正常工作。

Do I need to do something akin to json_encode from javascript before sending?在发送之前,我是否需要从 javascript 执行类似于 json_encode 的操作? Something that would convert everything to html entities?可以将所有内容转换为 html 实体的东西吗?

--Edit-- - 编辑 -

So I just discovered the "Net" tab in Firebug and it's showing me something I hadn't realized: it shows the GET request with all the proper data... But the server returns a 404. I don't know how that could happen since the PHP file is the same one that I grab stuff from 5 seconds before.所以我刚刚在 Firebug 中发现了“Net”选项卡,它向我展示了一些我没有意识到的东西:它显示了带有所有正确数据的 GET 请求......但是服务器返回了 404。我不知道那怎么可能因为 PHP 文件与我 5 秒前抓取的文件相同。

Also, even though I tried to change.getJSON() with.post(...,'json'), Firebug is still showing a GET request, not a POST, so it's got me even more confused now.此外,即使我尝试使用.post(...,'json') 更改.getJSON(),Firebug 仍然显示 GET 请求,而不是 POST,所以现在让我更加困惑。

You might also need to use encodeURIComponent since all of that data is going to get shipped in the URL string (the length might be an issue as well).您可能还需要使用encodeURIComponent ,因为所有这些数据都将在 URL 字符串中发送(长度也可能是一个问题)。

Use利用

$.post {(docTools+"?jsoncallback=?",{action:'update', loginCookie:loginCookie, page:thisFile, newdata:pagecontents}, function(data) {
  if(data.response[0].answer === 'true') {
   log('Page'+thisFile+' was saved correctly: '+data.response[0].details);
   $("#editcontents, #saveedit, #canceledit").remove();
   $("#bodycontents").html(pagecontents);
  } else {
   log('Failed to save page. Error (' + data.response[0].errorcode + ') : ' + data.response[0].errormessage);
  }
 },
  'json'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM