0

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.

4
  • 2
    Put the GET part at the end of the URL and data should be the XML input. Commented Feb 8, 2016 at 9:12
  • 1
    Check how string concatenation works in JS and use the comment above. Commented Feb 8, 2016 at 9:20
  • Thanks @Brainfeeder you mean this url: 'webService.php/GET' ? and also if data ` data: "xml"` instead of data: "a&b&xml" then how about other variables a and b.Thanks Commented Feb 8, 2016 at 9:25
  • 1
    @user5005768 Exactly :) Commented Feb 8, 2016 at 9:36

1 Answer 1

1

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);
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Rejith R Krishnan.Is there any way to see or print the final formatted data x=sender&b=receiver&xmlinput=<request><data1>100</data1><data2>200</data2></request> before sending to webservice.This would help to get aware of the correct format.thanks
@user5005768 : In that case make it type: "POST" to type: "GET"
hello @Rejith R Krishnan.will this code x=' + a + '&b=' + b or it will be this a=' + a + '&b=' + b. also why you don't send the data part.will this data part automatically concatenate by ajax? thanks
@user5005768 : I made an update. There was a mistake. With the new code you'll get something like the following. webService.php?xml=%3Crequest%3E%3Cdata1%3E%23inputData1%3C%2Fdata1%3E%3Cdata2%3E%23inputData2%3C%2Fdata2%3E&a=sender&b=reciever, which is the url encoded value of your xml and other data,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.