最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - correct JSONP Response - Stack Overflow

programmeradmin2浏览0评论

I'm trying locally to get JSONP to give me a correct response and pass it into my callback function jsonp_callback. Using code from: How do I set up JSONP?

header('content-type: application/json; charset=utf-8');
$data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); 
echo $_GET['jsonpCallback'] . '('.json_encode($data).')';

and

$.ajax({
    url: 'jsonp-response.php', 
    dataType:'jsonp',
    jsonp: 'jsonp_callback',
    success: function (r){
        console.log(r);
    }
});


function jsonp_callback (r){
    console.log('jsonp_callback:',r);
}

Sofar I'm getting a Response which looks like:

jQuery1102035954900085926056_1381230228656({"a":1,"b":2,"c":3,"d":4,"e":5})

Looking at the first answer from Testing a static jsonp response I think I'm doing it correctly but I'm not sure why jQuery gives me a unique string.

How would I make my response look like this ?

jsonp_callback({"a":1,"b":2,"c":3,"d":4,"e":5})

I'm trying locally to get JSONP to give me a correct response and pass it into my callback function jsonp_callback. Using code from: How do I set up JSONP?

header('content-type: application/json; charset=utf-8');
$data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); 
echo $_GET['jsonpCallback'] . '('.json_encode($data).')';

and

$.ajax({
    url: 'jsonp-response.php', 
    dataType:'jsonp',
    jsonp: 'jsonp_callback',
    success: function (r){
        console.log(r);
    }
});


function jsonp_callback (r){
    console.log('jsonp_callback:',r);
}

Sofar I'm getting a Response which looks like:

jQuery1102035954900085926056_1381230228656({"a":1,"b":2,"c":3,"d":4,"e":5})

Looking at the first answer from Testing a static jsonp response I think I'm doing it correctly but I'm not sure why jQuery gives me a unique string.

How would I make my response look like this ?

jsonp_callback({"a":1,"b":2,"c":3,"d":4,"e":5})
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Oct 8, 2013 at 12:16 invad0rinvad0r 9261 gold badge7 silver badges20 bronze badges 4
  • Is it possible that your response is a string as opposed to a JSON object? Can you check it? – JoeFletch Commented Oct 8, 2013 at 12:18
  • the success function returns an object – invad0r Commented Oct 8, 2013 at 12:26
  • Sorry, I mis-read your initial post. I see it now. I'm not familiar with setting the jsonp property, but did you try the following in the success property? success: function (r){ jsonp_callback(r); } – JoeFletch Commented Oct 8, 2013 at 12:36
  • including jsonp_callback(r) into success would work. – invad0r Commented Oct 8, 2013 at 12:52
Add a ment  | 

2 Answers 2

Reset to default 1

Here is the snippets from my code.. If it solves your problems..

Client Code :

Set jsonpCallBack : 'photos' and dataType:'jsonp'

 $('document').ready(function() {
            var pm_url = 'http://localhost:8080/diztal/rest/login/test_cor?sessionKey=4324234';
            $.ajax({
                crossDomain: true,
                url: pm_url,
                type: 'GET',
                dataType: 'jsonp',
                jsonpCallback: 'photos'
            });
        });
        function photos (data) {
            alert(data);
            $("#twitter_followers").html(data.responseCode);
        };

Server Side Code (Using Rest Easy)

@Path("/test_cor")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String testCOR(@QueryParam("sessionKey") String sessionKey, @Context HttpServletRequest httpRequest) {
    ResponseJSON<LoginResponse> resp = new ResponseJSON<LoginResponse>();
    resp.setResponseCode(sessionKey);
    resp.setResponseText("Wrong Passcode");
    resp.setResponseTypeClass("Login");
    Gson gson = new Gson();
    return "photos("+gson.toJson(resp)+")"; // CHECK_THIS_LINE
}

Like this:

$.ajax({
    url: 'jsonp-response.php', 
    dataType:'jsonp',
    jsonp: 'jsonp_callback',
    success: jsonp_callback 
});

function jsonp_callback (r) {
    console.log('jsonp_callback:',r);
}

In your code you never actually used the jsonp_callback function that you defined. You simply wrote some anonymous success callback. jQuery noticed that you used an anonymous function and that's why it generated this random name so that it can invoke the anonymous callback.

发布评论

评论列表(0)

  1. 暂无评论