0

I have used dojo/dom from a javascript file before to call a php file on another domain that handles some database queries and returns the result to the javascript file.

The call to the php file was (i offcourse hope i can use the same call to asp)

postdata = dojo.toJson({ action: "get",  userid: 1 });
require(["dojo/_base/xhr", "dojo/dom", "dojo/domReady!"],
function (xhr, dom) {
var xhrArgs = {
    url: "http://otherdomain.com/file.php",
    postData: postdata,
    handleAs: "text",
    load: function (result) { }
};
var deferred = dojo.xhrPost(xhrArgs);

In the php file i had

$foo = file_get_contents("php://input");
$postvalue = json_decode($foo, true);

to read the values from the dom call.

The reason i need to do this is because i get an error from the browser about a security risk because of the cross domain request.

So i think i need to use Jsonp

How do I write the php code in asp? NOT asp.net

Alan
  • 121
  • 2
  • 2
  • 12
  • i have also tried using $.ajax but i causes an mime type mismatch and i cant find any information about this exact situation where a javascript file calls a classic asp file and gets a mime type mismatch. – Alan Apr 08 '15 at 12:00

1 Answers1

0

Since your post data is JSON, the Request.Form won't be filled in, so you will have to use Request.BinaryRead and convert this result as a string.

See here to convert this JSON string into an object.

Community
  • 1
  • 1
  • My old php code call had alot of parameters including arrays. Thats why i used Json. I dont need to use Json with the asp call. – Alan Apr 08 '15 at 08:12
  • edit: i think i need to use Jsonp because of the cross domain security risk request – Alan Apr 08 '15 at 08:26
  • In that case you can send your data urlencoded and use [`Request.Form`](https://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx) in ASP to readout each parameter. – Wim Bruynooghe Apr 08 '15 at 08:26
  • About cross domain, you can set the [access control](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) http headers to allow another domain to access it – Wim Bruynooghe Apr 08 '15 at 08:28
  • I have tried the access control headers but cant get it to work in regular asp :( Please show me the lines if you have them :) – Alan Apr 08 '15 at 09:46
  • To give public access to all domains: `Response.AddHeader("Access-Control-Allow-Origin", "*");` `Response.AddHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");` – Wim Bruynooghe Apr 08 '15 at 11:25
  • Strangely enough those lines fail. Firstly ; at the end of just one line causes the file to fail completely. The () also causes the file to fail If i change Response.AddHeader("Access-Control-Allow-Origin", "*"); to Response.AddHeader "Access-Control-Allow-Origin", "*" it doesnt fail but it also doesnt work – Alan Apr 08 '15 at 11:56
  • Oh yeah my was code was in jscript, but you're using vbscript, so it should be indeed `Response.AddHeader "Access-Control-Allow-Origin", "*"` and `Response.AddHeader "Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS"` – Wim Bruynooghe Apr 08 '15 at 12:29
  • About your mime type mismatch, just add the correct content-type as well: for example `Response.AddHeader "Content-Type", "text/plain"` – Wim Bruynooghe Apr 08 '15 at 12:31
  • Yes i already changed the syntax to vbscript: I must admit i have never worked with mine types before since i never had a problem before. How do i know which mime type is the correct and should it be the first line in the ASP file? Should i also have a mime type line in the javascript file that calls the ASP file through AJAX? – Alan Apr 08 '15 at 13:13