使用post方法,json和ajax获取空指针异常

时间:2014-03-19 12:37:21

标签: jquery ajax json

服务器端

@Path("/insert")
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
        public RequestStatus insertData(String data){

            return saveData(true, data);
        }

客户端请求

function sendRequest(url,data){

        $.ajax({

        url: url,
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: data,
        async: true,

        error: function(jqXHR, textStatus, errorThrown) {
           console.log("error"); 

        },

        complete: function(jqXHR, textStatus) {
            console.log("completed");

        },

        success: function(data, textStatus, jqXHR) {
            if(data)
            if(data.status == 0){
                var value = JSON.parse(data.data);
                console.log("data saved");
                return;
            }
        }
    });

我正在传递JSON.stringify(jsonobj)作为数据。它还将此控制台输出作为错误返回

event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:8080/PrescriptionGenerator/webresources/PatientDataHandler/insert
couldnt save Patient.js:174
completed Patient.js:178

这是我的服务器日志

Mar 19, 2014 5:54:56 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Mar 19, 2014 5:54:56 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Mar 19, 2014 5:54:57 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@104ca48')
Mar 19, 2014 5:55:24 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.technoMedia.prescriptionGenerator.ApplicationConfig] in context with path [/PrescriptionGenerator] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.owlike.genson.reflect.BeanViewDescriptorProvider$BeanViewPropertyFactory.createAccessor(BeanViewDescriptorProvider.java:122)
    at com.owlike.genson.reflect.BeanPropertyFactory$CompositeFactory.createAccessor(BeanPropertyFactory.java:49)
    at com.owlike.genson.reflect.BaseBeanDescriptorProvider.provideMethodAccessors(BaseBeanDescriptorProvider.java:187)
    at com.owlike.genson.reflect.BaseBeanDescriptorProvider.provideBeanPropertyAccessors(BaseBeanDescriptorProvider.java:94)
    at com.owlike.genson.reflect.AbstractBeanDescriptorProvider.provide(AbstractBeanDescriptorProvider.java:106)
    at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:109)
    at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:74)
    at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:56)
    at com.owlike.genson.reflect.AbstractBeanDescriptorProvider$ContextualFactoryDecorator.create(AbstractBeanDescriptorProvider.java:79)
    at com.owlike.genson.reflect.AbstractBeanDescriptorProvider$ContextualFactoryDecorator.create(AbstractBeanDescriptorProvider.java:68)
    at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
    at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
    at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
    at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
    at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
    at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
    at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:58)
    at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:22)
    at com.owlike.genson.Genson.provideConverter(Genson.java:196)
    at com.owlike.genson.Genson.serialize(Genson.java:340)
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.writeTo(GensonJsonConverter.java:91)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:194)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:471)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:333)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:323)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:227)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.technoMedia.prescriptionGenerator.login.SessionFilter.doFilter(SessionFilter.java:134)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

我已经尝试重新启动服务器,清除缓存....它不能正常工作......我最好的选择是,ajax post方法存在一些问题.......

1 个答案:

答案 0 :(得分:0)

我不确定你要做什么,但这是我对ajax请求和响应的最佳做法。

formData = {
    param1: param1
}
$.ajax({
    type: 'POST',
    contentType: 'application/json',
    url: "http://localhost/test",
    dataType: "json",
    data: formData,
    success: function(data) {
        console.log(data);
        //success handler
    },
    error: function(data) {
        //error handler
    }
});

确保您的代码简单易懂,以便每个人都能理解。