如何GET / POST /删除/把信息与jQuery和AJAX [英] How to get/post/delete/put information with jQuery and AJAX

查看:106
本文介绍了如何GET / POST /删除/把信息与jQuery和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个DELETE,PUT,GET和POST使用Ajax和jQuery的请求。

该方法POST行之有效通过创建一个新的记录,但我不能使它工作的其他方法(PUT,DELETE和GET)。

这是在code(它工作正常,它创建了新的记录,但它并没有达到成功事件):

  VAR jsonExample = {广告:{称号:测试}};

$阿贾克斯({
            键入:POST,
            网址:http://example.com/advertisements.json
            数据:jsonExample,
            成功:函数(响应){
                    警报(测试);
            }
});
 

当我更改类型POST改为删除或PUT我有如下错误:

  

NetworkError:404未找​​到

当我把它改为GET它抛出了以下消息:

  

200 OK

但它没有任何其他反应。它应该是这样的:

<$p$p><$c$c>{"advertisement":{"created_at":"2012-04-17T13:20:17Z","from_age":null,"neighbourhood_id":null,"title":null,"date_to":null,"days":null,"promotion_id":null,"updated_at":"2012-04-17T13:20:17Z","date_from":null,"gender":null,"id":3,"display":null,"desc":null,"budget":null,"image":null,"to_age":null,"department_id":null,"town_id":null}}

请注意:我的应用程序越来越从远程服务器这个信息,但我不知道是否有一些与此有关的问题。因为我在谷歌浏览器中运行它,我已经收到了访问控制 - 允许 - 原产地消息浏览器的控制台上。

任何想法?

解决方案

你不能使用jQuery出于安全原因,跨域AJAX请求。然而,您可以使用 JSONP 提供您所请求的设置来处理数据的网址 JSONP 请求。

这个文章应该帮助你很多比我更是能够:<一href="http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/" rel="nofollow">http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/

I trying to do a DELETE, PUT, GET and POST a request with ajax and jquery.

The method POST works well by creating a new record, but I cannot make it work the other methods (PUT, DELETE and GET).

This is the code (it works fine, it creates the new record but it doesn't reach the "success" event):

var jsonExample = {"advertisement":{"title":"test"}};

$.ajax({
            type: "POST",
            url: "http://example.com/advertisements.json",
            data:jsonExample,
            success: function(response){
                    alert("test");
            }
});

When I change the type "POST" to "DELETE" or "PUT" I have the follow error:

NetworkError: 404 Not Found

And when I change it to "GET" it throws the following message:

200 OK

But it don't any other responses. It should be something like this:

{"advertisement":{"created_at":"2012-04-17T13:20:17Z","from_age":null,"neighbourhood_id":null,"title":null,"date_to":null,"days":null,"promotion_id":null,"updated_at":"2012-04-17T13:20:17Z","date_from":null,"gender":null,"id":3,"display":null,"desc":null,"budget":null,"image":null,"to_age":null,"department_id":null,"town_id":null}}

The

Please note: my app is getting this info from a remote server, but I don't know if that has something to do with this problem. Because I've run it in Google Chrome and I've received the Access-Control-Allow-Origin message on the browser's console.

Any ideas?

解决方案

You cannot make cross-domain AJAX requests using jQuery for security reasons. You may however be able to use jsonp providing that the URL you are requesting the data from is set up to handle jsonp requests.

This article should help you out alot more than I'm able to: http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/

这篇关于如何GET / POST /删除/把信息与jQuery和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆