-1

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 ?

4

1 回答 1

0

更新: 解决方案:在客户端:客户端希望将结果页面发送到要显示结果的位置。假设它是 result.jsp 。

在那个 result.jsp 中,获取参数值并显示。

In the server side : redirect the result to the page sent by the client.

为此我使用了 iframe,通过上述方法,可以从 iframe 读取值而不会出现跨域问题。

于 2013-08-07T13:02:27.547 回答