javascript AjaxFileUpload SyntaxError: missing } 在 XML 表达式中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3939741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:45:50  来源:igfitidea点击:

AjaxFileUpload SyntaxError: missing } in XML expression

javascriptajax

提问by Shwetanka

I'm trying to upload a file using $.ajaxFileUpload. My server script is returning a json object eg.

我正在尝试使用 $.ajaxFileUpload 上传文件。我的服务器脚本正在返回一个 json 对象,例如。

{"imgName": "test.jpg", "imgUrl": "/uploadtest/images/profile/sam.jpg"}

{"imgName": "test.jpg", "imgUrl": "/uploadtest/images/profile/sam.jpg"}

When I check in firefox it shows the correct response. Json is also received. But still I'm getting an error in alert:

当我检查 Firefox 时,它显示了正确的响应。Json也收到了。但我仍然收到警报错误:

SyntaxError: missing } in XML expression

I couldn't understand why this error is shown up. Also in firebug Json object is shown correctly.

我不明白为什么会出现这个错误。同样在萤火虫 Json 对象中也正确显示。

<script type='text/javascript' src='/js/ajaxfileupload.js'></script>
<script type='text/javascript'>
    function doFileUpload(){
        $("#loading")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });
        $.ajaxFileUpload(
            {
            url:'/json/image/upload.html?action=saveImage&nameSpace=tot',
            secureuri:false,
            fileElementId:'imgFile',
            dataType: 'json',
            success: function (data, status){
                alert("Success: "+data.imgUrl);
                },
            error: function (data, status, e){
                alert("Error: "+e+"---URL: "+data.imgUrl);
                }
            }
        )
    }
</script>

.... ....

…………

<div>
<strong>Upload Images:</strong><br>
<input type='file' name='imgFile' id='imgFile'>&nbsp;&nbsp;
<img src='/images/loading.gif' id='loading' height='60px' width='60px' style='display:none'>
<br><button name='upload' id='upload' onclick='return doFileUpload();'>Upload</button>
</div>

Anyone can tell me what's the reason for the Error?

任何人都可以告诉我错误的原因是什么?

回答by Shwetanka

I finally found the problem. The problem is with AjaxFileUpload plugin of Jquery which I'm using. Instead of 'json' in dataType it requires it to be capitalized. dataType: 'JSON'. Also after fixing this it automatically adds <pre> and </pre> to the beginning and end of the received json data. So it is not interpreted ad json.

我终于找到了问题所在。问题出在我使用的 Jquery 的 AjaxFileUpload 插件上。dataType 中的 'json' 需要大写。数据类型:'JSON'。同样在修复此问题后,它会自动将 <pre> 和 </pre> 添加到接收到的 json 数据的开头和结尾。所以它不是解释的广告 json。

Actual data that i received was

我收到的实际数据是

<pre>{"imgName": "test.jpg", "imgUrl": "/uploadtest/images/profile/sam.jpg"}</pre>

Now I'll have to remove there tags and then parse it with $.parseJson(). If anyone have the same error then check these problems. I hope ajaxFileUpload plugin will be fixed soon.

现在我必须删除那里的标签,然后用 $.parseJson() 解析它。如果有人遇到相同的错误,请检查这些问题。我希望 ajaxFileUpload 插件能尽快修复。

回答by Jude Adeline

I found that with the return data in Mozila I was having this problem. Actually the message was being returned with <p>message</p>and this was giving an error.

我发现使用 Mozila 中的返回数据时我遇到了这个问题。实际上,该消息正在返回,<p>message</p>并且这是一个错误。

The fix I applied was to remove anything not needed in the return message and it's working. But I'm not sure if its a permanent fix. At the end of the ajaxfileupload.js script file I modified the uploadHttDatafunction

我应用的修复是删除返回消息中不需要的任何内容并且它正在工作。但我不确定它是否是永久性修复。在ajaxfileupload.js脚本文件的最后我修改了uploadHttData函数

uploadHttpData: function( r, type ) {
    var data = !type;
    var dataparsed = r.responseText.split("{"); //added by Jude
    dataparsed = dataparsed[1].split("}"); //added by Jude
    ///Commented By Jude
    ///data = type == "xml" || data ? r.responseXML : r.responseText;
    // If the type is "script", eval it in global context
    data = type == "xml" || "{ " + dataparsed[0] + " }"; //added by Jude
    if ( type == "script" )
        jQuery.globalEval( data );
    // Get the JavaScript object, if JSON is used.
    if ( type == "json" ) {
        eval( "data = " + data );
    }
    // evaluate scripts within html
    if ( type == "html" )
        jQuery("<div>").html(data).evalScripts();
                    //alert($('param', data).each(function(){alert($(this).attr('value'));}));
    return data;
}

回答by Alaa Badran

I have fixed this,

我已经解决了这个问题

Just update the code line:

只需更新代码行:

xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;

With this one:

有了这个:

xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.textContent:null;

The innerHTML is causing the problem.

innerHTML 导致了问题。

回答by David Duncan

to reformat the pre from the response you can use regex(credit:https://github.com/carlcarl/AjaxFileUpload)

要从响应中重新格式化 pre,您可以使用正则表达式(信用:https: //github.com/carlcarl/AjaxFileUpload

    if ( type == "json" )
    {
        // If you add mimetype in your response,
        // you have to delete the '<pre></pre>' tag.
        // The pre tag in Chrome has attribute, so have to use regex to remove
        var data = r.responseText;
        var rx = new RegExp("<pre.*?>(.*?)</pre>","i");
        var am = rx.exec(data);
        //this is the desired data extracted
        var data = (am) ? am[1] : "";    //the only submatch or empty
        eval( "data = " + data );
    }

i found the solution to the json problem by searching github for ajaxfileupload, and found this version which worked correctly for me once I capitalized JSON

我通过在 github 上搜索 ajaxfileupload 找到了 json 问题的解决方案,并发现这个版本在我大写 JSON 后对我来说可以正常工作

回答by bbullis

Jude Adeline answer was correct if you were looking to return back a single dimensional json response. If you are looking to kick return a multi dimensional array from PHP, modify your code to this below.

如果您希望返回一维 json 响应,Jude Adeline 的答案是正确的。如果您希望从 PHP 返回多维数组,请将您的代码修改为下面的代码。

uploadHttpData: function( r, type ) {
    var data = !type;
    var dataparsed = r.responseText.substr(1); //changed-added

    dataparsedLength = dataparsed.length; //changed-added
    dataparsed = dataparsed.substr(0, dataparsedLength-1); //changed-added

    data = type == "xml" || "{ " + dataparsed + " }"; //changed-added
    // If the type is "script", eval it in global context
    if ( type == "script" )
        jQuery.globalEval( data );

    // Get the JavaScript object, if JSON is used.
    if ( type == "json" ) 
        eval( "data = " + data );

    // evaluate scripts within html
    if ( type == "html" )
        jQuery("<div>").html(data).evalScripts();

    return data;
}

回答by Nahed Kadih

i have been though this problem - u need to change only one line if ((type == "json") || (type == "JSON")) then works fine in both FireFox and IE

我一直在解决这个问题 - 你只需要更改一行 if ((type == "json") || (type == "JSON")) 然后在 FireFox 和 IE 中都可以正常工作

回答by kass

I had an error like this

我有这样的错误

I used:

我用了:

$Ex=end(explot('.', image));

in doajaxfileupload.php, which gave the error:

在 doajaxfileupload.php 中,它给出了错误:

"AjaxFileUpload SyntaxError: missing } in XML expression"

“AjaxFileUpload SyntaxError:在 XML 表达式中缺少 }”

I changed it to:

我把它改成:

$tmp=explot('.', image);
$Ex=end($tmp);

It's worked for me

它对我有用

The PHP documentationgives this example:

PHP文件给出了这样的例子:

mixed end ( array &$array );
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry
mixed end ( array &$array );
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry

回答by Vivian De Smedt

To solve the problem make sure you set the Content-Type of you answer to "text/html".

要解决此问题,请确保将答案的 Content-Type 设置为“text/html”。

That look strange indeed :-)

那看起来确实很奇怪:-)

The reason is that the answer is processed by Firefox to build a document. When the Content-Type of the answer is "text/plain" Firefox, that only display html, convert the answer into html by adding the tags.

原因是该答案由 Firefox 处理以构建文档。当答案的 Content-Type 为“text/plain”Firefox 时,只显示 html,通过添加标签将答案转换为 html。