I developed an application in play framework
for converting an uploaded csv
file to json
format .
Since this is a webservice , what I need is to create a client html page , to upload a file and convert it to json
format by using my web service.
But by using ajax
, getjson
or $.post
methods I failed to send the uploaded file to server.
My code is from client html is:
function myFunction()
{
alert("form data " + $("#form").serialize());
$.ajax({
url:"http://mydomain:9999/csvtojson",
type: 'POST',
data: $("#form").serialize(),
dataType:'json'
}).done(function (data) {
alert("data "+data);
$("#result").html(data);
});
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<form id="form" enctype="multipart/form-data" method="post" action="http://mydomain:9999/csvtojson" >
<input type="file" name="CSVFILE" id="file"/>
<button type="button" onclick="myFunction()">Click me</button>
<div id="result"></div>
</form>
Reason :
$("#form").serialize()
is empty for the form contain only files.
I tried another way to solve this problem by using iframe
.
I set the target json
to iframe
, and it successfully displays the result there.
But I can not access the data in that iframe
.
Since due to security reasons , browsers restricted accessing data from iframe , which is loaded from different remote site.
That's the cross domain policy restricting us. It's designed to prevent cross site scripting (XSS)
attacks.
Is there any way to solve my problem ?