简体   繁体   English

发表帖子的getJSON

[英]getJSON which does a post

I send a json object via ajax: 我通过ajax发送一个json对象:

function postJSON(){
$
.ajax({
    dataType : 'jsonp',
    jsonp : 'jsonp_callback',
    data : {
        message : "Hello World"
    },
    url : 'http://www.mypage.de/receiveJson.js',
    success : function() {
        alert("Success");
    },
});
}

How can i execute alert(message) on the serverside? 如何在服务器端执行alert(message)

Thanks 谢谢

getJSON which does a post 发表帖子的getJSON

JSON-P works by generating a <script> element. JSON-P通过生成<script>元素来工作。 <script> elements load external scripts with GET requests. <script>元素使用GET请求加载外部脚本。 There is no way to use JSON-P with a POST request. 没有办法使用带有POST请求的JSON-P。

(Well, not without using regular XHR, parsing out the JS function call from the response, and then using JSON.parse … but then you should just set up the server to use JSON). (好吧,不是没有使用常规XHR,从响应中解析出JS函数调用,然后使用JSON.parse ......但是你应该只设置服务器使用JSON)。

How can i execute alert(message) on the serverside? 如何在服务器端执行警报(消息)?

You'd need to pass the code to something server side that would evaluate it. 您需要将代码传递给评估它的服务器端。 You could use node.js if you want to evaluate JavaScript. 如果要评估JavaScript,可以使用node.js。 However: 然而:

  1. Allowing arbitrary code to be submitted to your server for execution is very dangerous 允许将任意代码提交给服务器执行是非常危险的
  2. alert is a browser API that makes little sense on the server, you would have to define an implementation of alert that did something sensible (such as sending an email or logging to a database). alert是一个在服务器上没有意义的浏览器API,你必须定义一个做出明智的事情的alert实现(例如发送电子邮件或记录到数据库)。

* EDIT * *编辑*

Rewriting because someone pointed out the server-side applications of Javascript to me. 重写是因为有人向我指出了Javascript的服务器端应用程序。 I was aware of node.js but kind of ignored the possibility you were doing javascript on the serverside for real. 我知道node.js,但有点忽略了你在服务器端做javascript的可能性。 At any rate, unless your special flavor of serverside javascript works differently than the client-side implementation I'm familiar with, alert() is probably a bad call for serverside code, because it blocks the running code and tries to pop a dialog. 无论如何,除非你特别喜欢的服务器端javascript与我熟悉的客户端实现不同,alert()可能是对服务器端代码的错误调用,因为它会阻止正在运行的代码并尝试弹出对话框。 If there's an option to write to the console I would do that instead. 如果有一个写入控制台的选项,我会这样做。

Additionally, I would sincerely hope you could not directly trigger an alert() call on the server from your ajax code. 另外,我真诚地希望你不能从你的ajax代码直接触发服务器上的alert()调用。 There would have to be some kind of method on the server in order to generate the alert. 服务器上必须有某种方法才能生成警报。

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

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