无法在Ajax中获得Java响应,数据类型:"json "但是dataType: "tex

Unable to get Java Response in Ajax with dataType: "jsonp" but dataType: "text" Works

本文关键字:quot json tex dataType 但是 数据类型 Ajax 响应 Java      更新时间:2023-09-26

当我使用以下代码时,我无法在ajax中获得Callback响应值

$.ajax({
    type: 'POST',
    jsonpCallback: 'jsonCallback',
    contentType: 'application/json',
    url: apiurl,
    dataType: "jsonp",
    data: jsonstring,    
    success: function(response)
    {                 
        callback(response);
        token = response;                
    },
        error: function(jqXHR, textStatus, errorThrown){
        alert('Not Permitted  ' + textStatus);
    }

但是当我改变上面的dataType: "text"它工作。

当我使用dataType:"jsonp"时如何获得响应。

我的java响应如下:

返回Response.status (200) .entity("海").build ();

如果你使用jsonp似乎没有必要设置contentType: 'application/json',因为它实际上是一块javascript而不是json字符串。