Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't do a POST via jquery #3

Open
predator747 opened this issue Aug 13, 2014 · 2 comments
Open

Can't do a POST via jquery #3

predator747 opened this issue Aug 13, 2014 · 2 comments

Comments

@predator747
Copy link

Hi,

I love the idea of our plugin but it is not possible to send POST request via Ajax and jquery.
I do always get the following exeption:

Chrome Debug:
400 (This page expects a form submission)

Jenkins:
"java.lang.reflect.InvocationTargetException"
"Caused by: javax.servlet.ServletException: This page expects a form submission but had only {...}"

I send the POST like this:
var jqxhr = $.post(
"http://xxy.xy.xyx.xy:8080/job/AddPage/build/api/json",
{"parameter":[{"name":"s_space","value":""},{"name":"s_page","value":""},{"name":"s_parent","value":""}],"statusCode":"303","redirectTo":"."},
"json"
)

Any Idea?

Thanks for helping me :)

@predator747
Copy link
Author

Ok i got it it works. You need set form data key name as 'json='. You could do it like this:

var jsonParams = '{"parameter": [{"name": "xy", "value": "1"}, {"name": "page", "value": "2"}, {"name": "parent", "value": "3"}]}';
var data = 'json='+jsonParams;
$.post(url , data );

It works thanks!

@ask-SAK
Copy link

ask-SAK commented Jun 14, 2018

Hi All,

If anyone was still looking for this to work below is a code that works for sure.
Important info to be noted is

  1. master_testlist is a form data in JSON format
  2. as you can see while declaring form_data, the job_data has to be again stringified and only then this worked.
var job_data = JSON.stringify(master_testlist)
var req = $.ajax({
         url: 'https://jenkins.test.local/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)',
         type: 'GET'
     });
     req.then( function(data4){
        crumb = data4.split(':')
        var form_data='name=Data&value='+job_data+'&statusCode=303&redirectTo=.&Jenkins-Crumb='+crumb[1]+'&json={"parameter": {"name": “Data”, "value":'+JSON.stringify(job_data)
        form_data=form_data+'},"statusCode": "303","redirectTo": ".","Jenkins-Crumb": '+crumb[1]+'}&Submit=Build'
        var encoded_data=encodeURI(form_data)
        var req1 = $.ajax({
                    url: 'https://jenkins.test.local/job/srini-test1/build?delay=0sec',
                    type: 'POST',
                    data: encoded_data, 
                    beforeSend: function(xhr){
                        xhr.setRequestHeader(crumb[0],crumb[1]);
                    }
                    });
             req1.then(function(data5){
                 console.log(data5)
                 document.getElementById("Save_Result").innerHTML='<BR>Save Successful<BR>'
             }, function(e){ console.log(e); document.getElementById("Save_Result").innerHTML='<BR>Save failed<BR>'} );
                 
     }, function(e){ console.log(e); document.getElementById("Save_Result").innerHTML='<BR>Error getting crumb<BR>'} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants