0

I have prepared an application with image upload. It sends some text and an image.

It's working fine when I add an image and content(text). Unfortunately it's not working when I call the service without an image, see code below,

var params = {
        file :$.selectedImageVw.image,   //if file is not selected it will send as null
        UserId : Ti.App.userID,
        postContent : $.postMessage.value
    };
var xhr = Titanium.Network.createHTTPClient();
    xhr.onreadystatechange = function() {
        if (this.readyState == 4) {
            progressVw.hide();

            // callback("Success");
            // alert(this.responseData);
            progressVw.hide();
            xhr = null;
        }
    };

    xhr.setRequestHeader('Content-Type', 'multipart/form-data');
    xhr.setRequestHeader('enctype', 'multipart/form-data');
    xhr.setRequestHeader('Content-length', params.length);
    xhr.open("POST", "uploadUrl");
    xhr.send(params);

I hope someone can help me. Thanks in advance!!

Amit
  • 13,134
  • 17
  • 77
  • 148
Mohanraj S K
  • 657
  • 4
  • 16

2 Answers2

0

Try to use a service like http://requestb.in/ to check if the requests made by the client are the issue or the backend you use.

Fokke Zandbergen
  • 3,866
  • 1
  • 11
  • 28
  • Hi @FokkeZandbergan, Thanks for ur reply, I have test with Postman Rest Client(Google API). But with the same parameters and working good. When done with mobile only not working. – Mohanraj S K Jul 01 '15 at 11:00
  • If I understand you correctly you've tested it with a different client. But what I suggest is that you use requestb.in to test the Titanium client with a different server. That way you can see exactly how the request is coming in and e.g. compare that to using Postman Rest Client on that same requestb.in URL. – Fokke Zandbergen Jul 02 '15 at 06:27
  • @FokkeZandbergan i ave tracked error by back end developer, and they return what i am sent from front end. when i send with a empty image they received nothing which mean additiona params like userId also not recieved. i really don’t know how to resolve this. ): – Mohanraj S K Jul 06 '15 at 07:47
0

@FokkeZandbergan thanks for your respone, This issue resolve by very simple modification'

xhr.setRequestHeader('Content-Type', 'multipart/form-data');

changed to

xhr.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");

Now its working with both imags and without images.

It may help to some one . :)

Mohanraj S K
  • 657
  • 4
  • 16