user2289728
user2289728

Reputation: 31

getJSON which does a post

I send a json object via ajax:

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?

Thanks

Upvotes: 0

Views: 88

Answers (2)

Matt
Matt

Reputation: 1186

* EDIT *

Rewriting because someone pointed out the server-side applications of Javascript to me. I was aware of node.js but kind of ignored the possibility you were doing javascript on the serverside for real. 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. 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. There would have to be some kind of method on the server in order to generate the alert.

Upvotes: 1

Quentin
Quentin

Reputation: 943097

getJSON which does a post

JSON-P works by generating a <script> element. <script> elements load external scripts with GET requests. There is no way to use JSON-P with a POST request.

(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).

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. 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).

Upvotes: 1

Related Questions