5

Here's what i have tried soo far..

<html>
  <head>
    <title>bugstats.com</title>
  </head>
<script type="text/javascript"     src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery-json.googlecode.com/files/jquery.json-    1.3.min.js"></script>
<script type="text/javascript" >
function hello(){
var myObject = {"method":"User.login", /* is this the right method to call? */
"params":[  { "login" :"user", /*should i include the login credentials here? */
"password" : "pass123" , 
"remember" : "True"} ]  };
var enc = $.toJSON(myObject);

$.ajax({"contentType":"application/json",
    "data": enc, 
    "crossDomain":"true",
    "dataType": "json", 
    "url": "https://bugzilla.company.com/bugzilla/jsonrpc.cgi", /* is this correct or should it be https://bugzilla.company.com/bugzilla/jsonrpc.cgi?method=User.login? */ 
    "type": "POST",
    success: function(){
            alert("Hallelujah");
                console.log(arguments); 

             },
    error: function () {
    alert("Failed")
    }

   });
}
function parseResponse(obj){
 alert("Success")
 console.log(obj)
}
</script>
  <body>
    <h1>bugzilla.com</h1>
    <input type="button" onclick="hello()" value="Click">
</body>

Reading upon this JSONPRC, not getting far.

When i click the button - make a call, to login/do anything for that matter - i get the following error -

OPTIONS https://bugzilla.company.com/bugzilla/jsonrpc.cgi 403 (Forbidden) jquery.min.js:19
XMLHttpRequest cannot load https://bugzilla.company.com/bugzilla/jsonrpc.cgi. Origin http://172.16.229.137 is not allowed by Access-Control-Allow-Origin.

From my understanding, "Access-Control-Allow-Origin" is caused because of "same origin policy" problem and hence i should use "jsonp". But,Jsonp - i.e, script injection can only be done via GET request. But,if i try the same JS script with a GET request - i get the following :

code: 32610
message: "For security reasons, you must use HTTP POST to call the 'User.login' method."

Confused on how to connect/login via Web services, i'm clearly doing something silly something majorly wrong here.. would be a great deal of help if anyone can help me connect and fetch the bug details.I've been at it since 8-10 days now.. :(

FYI:

  • I do not have access to the server

  • I'm on a client setup and accessing the Bugzilla server

Other links,

Ajax Call

Loggin In

BugzillaApc

Google Groups - Live conversation

Vivek Chandra
  • 4,240
  • 9
  • 33
  • 38

1 Answers1

5

You need to be using the Bugzilla_login and Bugzilla_password parameters to authenticate every call, which will be GET using jsonp. Your call will look like the following, using User.get as an example:

// Method parameters
var params = [{
  /* The authentication parameters */
  "Bugzilla_login": "YourUserName",
  "Bugzilla_password": "YourPassword",
  /* The actual method parameters */
  "ids": [1, 2]
}];
var myObject = {
  "method": "User.get",
  "params": JSON.stringify(params)
};

$.ajax({"contentType": "application/json",
    "data": myObject, /* jQuery will handle URI encoding */
    "crossDomain": "true",
    "dataType": "jsonp", /* jQuery will handle adding the 'callback' parameter */
    "url": "https://bugzilla.company.com/bugzilla/jsonrpc.cgi", 
    "type": "GET",
    ...

You have to do it this way because:

  • You will be making a cross-domain call
  • Because you can't set things like Access-Control-Allow-Origin you'll have to do it via jsonp (or proxying, but jsonp is simpler)
  • jsonp is necessarily a GET request, not POST

The relevant docs:

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • If i try the above code snippet,i.e. Raw `myObject` for jQuery to process - i get the error => `code: 32000 message: "Could not parse the 'params' argument as valid JSON. Error: malformed JSON string, neither array, object, number, string or atom, at character offset 1 (before "object Object]") at Bugzilla/WebService/Server/JSONRPC.pm line 169. Value: [object Object]"` , But the json string i've given is as per the prescribed method. If i include the `toJson` method before sending - i get `please include a method before sending a request` error comes up.. – Vivek Chandra Aug 01 '12 at 06:21
  • @VivekChandra Oops, coded that up a bit too fast, please see the update which should have all the right encodings in the right places. – blahdiblah Aug 01 '12 at 22:38
  • It worked.. :) .. thanks a million, but have a small question - in the [__Doc__](http://www.bugzilla.org/docs/4.2/en/html/api/Bugzilla/WebService.html#CALLING_METHODS)'s it says even an array is accepted!, so why is string being accepted and not when params was an array??.. – Vivek Chandra Aug 03 '12 at 08:27
  • @VivekChandra Arrays are accepted, but must be URI encoded. See the "`ids`" parameter in the call above for an example. – blahdiblah Aug 03 '12 at 17:37
  • so, `json.stringify` makes it into a string - which is URI encoded?.. and,hence it works ?.. i'm not aware of URI encoding,gotta look into it.. if thats where i was going wrong!,damn.. i should've read the doc's more thoroughly.. :( – Vivek Chandra Aug 03 '12 at 18:13
  • @VivekChandra `JSON.stringify` turns it into a JSON string, jQuery automatically does the URI encoding to the `data` object. – blahdiblah Aug 03 '12 at 18:21