1

ajax 呼び出しを行っている spring mvc コントローラーから Map を返そうとしていますが、正しい応答が得られません。

構成ファイルで mvc 注釈タグを使用し、ライブラリに jackson jar ファイルも含めました。

私にとっての要件は、Map を Ajax 呼び出しの成功に戻すことです。これにより、html のテーブル行を変更できます。

コントローラーのコード:

@RequestMapping(value="/pricingrecall.do", method=RequestMethod.POST)
    @ResponseBody
    public Map<Integer,String>  pricingUpdate(@RequestParam(value = "opp_Code", required = false) String opp_Code,
            @RequestParam(value = "ref_id", required = false) String ref_id,
            ModelMap model,
            HttpServletRequest request, HttpServletResponse response) throws SQLException, Exception{

            String User="fe0777";
        List<CrossListViewBean>updatedRow = new ArrayList<CrossListViewBean>();
        //String message="";
        logger.info(methodLocation+"|"+"Calling pricing recall ....");
        Map<String, Object> result = new HashMap<String, Object>();
        updatedRow=crossCampService.getupdatedrowListview(opp_Code, ref_id, user);
        Map<Integer,String> lbean= new HashMap<Integer,String>(); 
        lbean=crossCampService.getUpdatedDataPosition(updatedRow.get(0));
        return lbean;

    }

Ajax からの呼び出し:

                                    jQuery.ajax( {                                         
                                    url : '/Web/pricingrecall.do',
                type: "POST",
                cache : false,
                timeout : 60000,
                data : {
                    opp_Code :CampId ,
                    ref_id : index
                },
                success : function(result, textStatus, request) {
                    if(result)
                    {   
                        alert(result);
                        //jQuery(".note"+index).html(data);

                    }else
                    {
                        alert("The user session has timed out. Please log back in to the service.");
                        window.location.replace("logout.do");
                    }
                },
                error : function(request, textStatus, errorThrown) {
                    alert("The system has encountered an unexpected error or is currently unavailable. Please contact the support number above if you have any questions.");
                }
            });

ここで ajax の成功では、常にエラーが発生し、エラー文字列に転用されます。ajax成功でMAPからJsonを取得するにはどうすればよいですか

助けてください

4

2 に答える 2

0

jackson-databind を使用し、コントローラー メソッドに @ResponseBody アノテーションを使用すると、返されたデータが自動的に json に正常に変換されました。Maven を使用する場合は、これらの依存関係を pom.xml に追加します (jackson.version は私にとって 2.4.0 です)。

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>

それ以外の場合は、jar ファイルをクラスパスに追加できます。

于 2015-05-21T06:37:42.660 に答える
0

私はflexjsonを使ってjson出力を正しくしています。flexjson を使用した私のサンプル コードを添付します。これを参照として使用し、コントローラー メソッドを再構築して、正しい json を出力できます。このリンクは、マップをシリアル化する方法に役立ちます。

@RequestMapping(value = "/{id}", headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> findUser(@PathVariable("id") Long id) {
     User user = userService.find(id);

     HttpHeaders headers = new HttpHeaders();
     headers.add("Content-Type", "application/json; charset=utf-8");

     return new ResponseEntity<String>(user.toJson(), headers, HttpStatus.OK); 
}

@Entity
public class AppUser {
    @NotNull
    private String firstName;

    @NotNull
    private String lastName;

    //Getter Setter goes here

    public String AppUser.toJson() {
       return new JSONSerializer().exclude("*.class").serialize(this);
    } 
}
于 2012-12-04T10:44:20.103 に答える