How to use JSONP to solve the XSS problem?

I have a javascript part running on a berth server that sends XMLHTTPRequest to scoket on another server (wamp server). The request is sent to the socket, however the XHR response seems to be blocked.

I heard that I can use JSONP to solve this problem. However, since I am new to javascript and I never used the JSONP technique before I would really appreciate any help on using this technique?

function sendPost(url, postdata, callback) {

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
} 

xmlHttp.onreadystatechange=callback
xmlHttp.open("POST",url,true)
xmlHttp.send(postdata);

}

function sendInitRQ(width, height) {

var post = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><command     type=\"init\"><width>" + width + "</width><height>" + height + "</height></command>";

sendPost("http://localhost:80/socket.php", post, initReturned);

}

I know that a php socket receives a message, as when I check the server log, I get 200 receive requests.

I just want to know how can I use the JSONP approach? I have seen examples of the approach, but I still don't know how to do this.

+3
1

JSONP HTTP- . . URL-, HTTP "GET" JSON, . , JSONP ; , .

, <script>, "src" URL- JSONP. URL , Javascript, , JSON. (, , , " ", , "jsonp".) , , . ,

function handleJSON(json) {
  var something = json.something;
  // ... whatever ...
}

URL- "handleJSON", :

handleJSON({"id": 102, "something": { "more": "data", "random": true }});

, <script> URL- "src", , ( ), .

, JSONP , , , (), .

edit — : http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

+7

Source: https://habr.com/ru/post/1747363/


All Articles