user5005768 February 2016

How can i post combined variable and xml data using jquery ajax for webservice

I am new in ajax, xml and web service. I want to post http request to call webservice with some variable values and xml data with following format:

x=sender&b=receiver&xmlinput=<request><data1>100</data1><data2>200</data2></request>

Here is my ajax code:

//These are the parameters      
var a = 'sender';       
var b = 'receiver';
var xmlinput = '<request><data1>$('#inputData1').val()</data1><data2>$('#inputData2').val()</data2>';       
$.ajax({
    url: 'webService.php',
    type: "POST",   
    async: true,
    data:  "a&b&xml" , 
    dataType: 'xml',
    contentType: "text/xml",
    success: function(xmlData){             

    },                      
    error: function(jqXHR, textStatus, errorThrown) {
       console.log(textStatus, errorThrown);
    }
});

Please help me to do that.Thanks.

Answers


Rejith R Krishnan February 2016

There are a couple of things you need to look into. In JavaScript use + operator for concatenating strings andcontentType: "text/xml" means that JavaScript expects the data value in XML format.
Note : Unlike in PHP, using variable name in single quoted string, doesn't get processed by javascript. It's treated as a simple string.

var a = 'sender';       
var b = 'receiver';
var xmlinput = '<request><data1>' + $('#inputData1').val() + '</data1><data2>' + $('#inputData2').val() + '</data2>';       
$.ajax({
    url: 'webService.php' ,
    type: "GET",   
    async: true,
    data: {
        xml : xmlinput,
        a: a,
        b: b
    }, 
    dataType: 'xml',
    contentType: "text/xml",
    success: function(xmlData){             

    },                      
    error: function(jqXHR, textStatus, errorThrown) {
       console.log(textStatus, errorThrown);
    }
});

Post Status

Asked in February 2016
Viewed 1,544 times
Voted 10
Answered 1 times

Search




Leave an answer