0

I'm sure this is a basic syntax error but I'm trying to make a rest call using jQuery mobile ajax (code below) and as far as I can tell the ajax is not triggering.

        function triggerCall() {
            alert("function triggered");
            $.ajax({
                type: "GET",
                dataType: "jsonp",
                url: REST Url,
                success: function (data) {
                    alert(data);
                }
            });

Any help would be appreciated

Ben Pearce
  • 6,884
  • 18
  • 70
  • 127

3 Answers3

0

if you're actually writing url: REST Url, then you must change it to either a var, or a string.. that would be a problem

Kristian
  • 21,204
  • 19
  • 101
  • 176
0

I believe that if you're using JSONP, you need to specify the callback in the $.ajax request and then again in your REST file return. Here is an example of what I've been using succesfuly (It's not perfect though, I'm sure).

$.ajax({
    url: 'www.domain.com/string/to/your/REST/api',
    data: {
        dataToBeSent: variable, 
        dataToBeSent: sessionStorage.getItem('local/session Storage'),
        dataToBeSend: "or a string"
    },
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function(data){    
        alert("Huzzah!");
    },
    error: function(){
        alert("Boohisssss");
    }
}); //end ajax call

And then in the url, I'd place this code at the bottom of the file:

header("Content-type: application/json", true);
echo $_GET['jsoncallback'] . '(' . json_encode($data) . ');';
exit;

Where data is a array that is JSON encoded, using json_encode() in PHP, and then wrapped up in a callback function (the $_GET['jsoncallback'])

Like I said, it's not perfect, but it's been working for me.

TheBrockEllis
  • 979
  • 9
  • 19
0
  1. Check for errors in browser console. eg. in chrome menu>tools>javascript console.
  2. Also recommend adding an error handler to your ajax call: http://api.jquery.com/error/
  3. Determine which method, JSONP or CORS, to use for your restful ajax calls: So, JSONP or CORS? .
Community
  • 1
  • 1
Brian Cajes
  • 3,274
  • 3
  • 21
  • 22