开发者

valums Ajax-Upload onComplete responseJSON always is empty !

I'm using ajax-upload in a ASP.Net forms app.

I need to send a string hash which identifies the file once it is saved on the server.

I'm doing it in the ashx handler emitting a json string, but no matter what i send from the server, in the javascript i'm getting an empty object... what am I doing wrong?

This is the snippet for the on load method on the ashx // EDITED context.Response.ContentType = "application/json"; context.Response.Conten开发者_如何学运维tEncoding = System.Text.Encoding.UTF8; context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

ProcessFile(context.Response);
context.Response.Write("{\"success\": true, \"hash\": " + _myHash + "}");

And this is the on complete

onComplete: function(id, file, responseJSON) {
    console.log(responseJSON);
}


Did you read server/readme.txt included in the package?

You should return json as a text/html


Use property responseType of AjaxUpload function. Set it to "json".

Like this:

new AjaxUpload("#importButton",{
  action: "upload.html",
  name: 'name',  
  responseType: "json",
  allowedExtensions: ['xml'],
  onSubmit : function(file, ext){
  },
  onComplete: function(file, response){
  }  
}); 


These are the headers I set when returning JSON data:

context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

When they are not correct, I've seen strange behavior from various browsers. If changing these does not help, I would try to eliminate the ajax post, to verify that you are infact sending the correct data.

If you're using jQuery, the $.ajax() method has a noCache option you may wish to investiage as well.


after I debuged the uploader's code, i discovered the problem, this happened to me, the problem is the response's parse, so, you must put a double quote on the hash to, like this:

"{\"success\": true, \"hash\": \"" + _myHash + "\"}"


These are the changes i had to do to the fileuploader.js file so it could work:

    this.log("innerHTML = " + doc.body.innerHTML);      
    // replace <pre> tags from IE Frame
    var content = doc.body.innerHTML.replace(/<pre>/gi, '').replace(/<\/pre>/gi, '');

    try {
        response = eval("(" + content + ")");
    } catch(err){
        response = {response: content};

I also changed the way it emits the response in case of JSON errors:

        var response = xhr.responseText;

        try {
            response = eval("(" + xhr.responseText + ")");
        } catch(err){
            // emit an object with the offending responseText
            response = {responseText: response};
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜