skip to Main Content

I’m executing an ajax call to a external api (this cannot be modified) to upload an store a file into a folder. This request must return a path (ex. "C:DoctosFile.pdf" but after a console.log is returning something like this:

#document < string xmlns="http://tempuri.org/">"C:DoctosFile.pdf"

So my question is, what can I do to get only the text that I want without any change in the api (because I’m not able to do it).

Here is the ajax call that I’m using.

PD. This ajax call is using the provided structure for the dev team that developed the api so things like dataType also cannot be modified

var data = new FormData();
var files = $('#fileUpload').get(0).files;

if (files.length > 0) {
    data.append("UploadedFile", files[0]);
}

$.ajax({
    type: 'POST',
    url: 'api/v1/moreurl/UploadFile',
    contentType: false,
    processData: false,
    data: data,
    success: function (data) {
        var res = data;
        //Returns above example
        console.log(res);

        //Returns something like <p>[object XMLDocument]</p>
        $('#MyInput').attr('src', res);
    }
});

2

Answers


  1. I would use regular expressions to get the desired string from received data. Put this after success line.

    var regex = />"(.*)"/;
    var matched = regex.exec(data);
    var result = matched[1];
    console.log(result);
    

    The regex matches the last quoted string in your example.

    Login or Signup to reply.
  2. You can get the data in the xml with jQuery

    $.ajax({
        type: 'POST',
        url: 'api/v1/moreurl/UploadFile',
        contentType: false,
        processData: false,
        data: data,
        success: function (data) {
            // Get the contents of the xml
            var file = $(data).find('string').text();
            $('#MyInput').attr('src', file);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search