javascript - 将图像从移动网络应用程序(PhoneGap 和 JavaScript)上传到 SOAP 网络服务返回状态 0

标签 javascript soap xmlhttprequest

我看过很多例子和论坛,但我还不清楚。 我想用手机拍照,然后将它发送到 WebService。
在 WebService 中我有一个函数 UploadImage将图像保存在网站中。这个函数的参数是:

 <UploadImage xmlns="http://*********">
  <urlSite>http://********</urlSite>
  <iListName>{*************}</iListName>
  <iUpdateMode>*******</iUpdateMode>
  <iMetaData><Fields><Field Name=”Title” >Title of the Image</Field></Fields></iMetaData>
  <iAttachments><Attachments><Attachment Name=”Name of the file”>file in the format Base64String</Attachment></Attachments></iAttachments>
  <iOverWrite>****</iOverWrite>
</UploadImage>  

发帖请求:

function sendImage ()
{
    var wsUrl = "*******?op=UploadImage";
    var soapRequest ='<?xml version="1.0" encoding="utf-8"?> \
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" \
         xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \
  <soap:Body> \
    <UploadImage xmlns="**********"> \
      <urlSite>**********</urlSite> \
      <iListName>{******}</iListName> \
      <iUpdateMode>********</iUpdateMode> \
      <iMetaData><Fields><Field Name=”Title” >Title of the Image</Field></Fields></iMetaData>
      <iAttachments><Attachments><Attachment Name=”Name of the file”>file in the format Base64String</Attachment></Attachments></iAttachments> \
      <iOverWrite>*****</iOverWrite> \
    </UploadImage> \
  </soap:Body> \
</soap:Envelope>';
    var xmlhttp =  createXMLHttpRequest();


xmlhttp.open("POST", wsUrl, true,"username","password");

xmlhttp.setRequestHeader ("Post", "********");
xmlhttp.setRequestHeader ("Host", "*****");
xmlhttp.setRequestHeader ("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", soapRequest.length);
xmlhttp.setRequestHeader ("SOAPAction", "******");


xmlhttp.onerror = function(e) {
    alert("Error ocurred. Error = " + e.message);
}

xmlhttp.ontimeout = function(e) {
    alert("Timeout error!");
}

xmlhttp.onreadystatechange  = function () { 
    alert(xmlhttp.readyState);

    if (xmlhttp.readyState==4)
    {
        alert(xmlhttp.responseText);
        alert(xmlhttp.status);
        // if "OK"
        if (xmlhttp.status==200 || xmlhttp.status==0)
        {
            alert(xmlhttp.responseXML);
        //[Get xmlhttp.responseXML.xml and do something with it]

        }
        else
        {
        //[Get xmlhttp.responseXML.xml and do something with it in the case of an error]
        }
    }
}


xmlhttp.send(soapRequest);

状态 t=returned 为 0。
在标签 <iAttachments><Attachments><Attachment Name=”Name of the file”>file in the format Base64String</Attachment></Attachments></iAttachments>我应该发送一个Base64格式的文件,但是我不知道如何把这个格式的文件放到这个标签里,所以我发送了一个String,可能是问题的原因吧!

我尝试用 jquery.ajax 发布它:

function sendReq()
{    var wsUrl = "*******";
    var soapRequest ='<?xml version="1.0" encoding="utf-8"?>'+
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
  '<soap:Body>'+
    '<UpdateImage xmlns="*******">'+
      '<urlSite>*********</urlSite>'+
      '<iListName>{*****}</iListName>'+
      '<iUpdateMode>****</iUpdateMode>'+
      '<iMetaData><Fields><Field Name="Title" >'+my_var1+'</Field></Fields></iMetaData>'+
      '<iAttachments><Attachments><Attachment Name="Nom du fichier 1">'+my_var2+'</Attachment></Attachments></iAttachments>'+
      '<iOverWrite>*****</iOverWrite>'+
    '</UpdateImage>'+
  '</soap:Body>'+
'</soap:Envelope>';
   $.ajax({
                    type: "POST",
                    url: wsUrl,
                    contentType: "text/xml; charset=utf-8",
                    dataType: "xml",
                    data: soapRequest,
                    beforeSend: function (xhr){ 
        xhr.setRequestHeader('Authorization', make_base_auth('username', 'password')); 
        xhr.setRequestHeader("SOAPAction", "****/UpdateImage");
    },                
    contentType: "text/xml; charset=utf-8",

                success: processSuccess,
                error: processError
            });
return false;

结果是:req.responseText=undefinedstatus=error

最佳答案

我会使用一些在线工具(例如 http://www.opinionatedgeek.com/dotnet/tools/base64encode/)对文件进行 base64 编码,然后将字符串硬编码到您的 xml 中。先看看这是否有效,以检查您的其余代码是否正常。

如果可行,请查看这个问题,了解如何在发送文件之前对文件进行 base64 编码: Get Base64 encode file-data from Input Form

此外 - 调试代理在这种情况下非常有用,因此您可以看到正在发送的实际请求。如果您有一些已经适用于此服务的东西,您可以使用类似 fiddler ( http://fiddler2.com/ ) 的东西来获取请求并将其与您的请求进行比较。

关于javascript - 将图像从移动网络应用程序(PhoneGap 和 JavaScript)上传到 SOAP 网络服务返回状态 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15026024/

相关文章:

javascript - 在本地 html 文件中使用 XMLHttpRequest 加载 'server-side' 文件

javascript - jquery 处理来自外部 iframe 的事件

javascript - 如何使物化日期选择器(实际上是 pickadate)可编辑?

javascript - 如何从 Ecmascript 6 (ES6) 上的类对象中的事件回调函数访问对象成员

javascript - 使用 npm 请求向 SOAP api 发送帖子

linux - 在 Linux 上编写 SOAP 服务 - 需要工具和帮助

c++ - Gsoap Http 事件回调

javascript - Javascript 同步 XMLHttpRequest 的替代方案(在 Safari 中超时)

javascript - 如何在不使用 XDomainRequest 或 XMLHttpRequest 的情况下使用自定义 header 发出跨域请求?

javascript - 格式化 Json 文件中的 Json 对象值